pytest-bdd icon indicating copy to clipboard operation
pytest-bdd copied to clipboard

Where the scenarios shortcut should be place?

Open mbarakaja opened this issue 7 years ago • 15 comments
trafficstars

For me, in the documentation is not clear if I have to use scenarios on each step file or if I can use it in a single place once.

I have tried calling it inside the conftest.py module, which is the first file loaded by pytest but nothing happened.

mbarakaja avatar Jul 22 '18 16:07 mbarakaja

yes likewise - the documentation is very unclear, and provides no example. can someone please provide some clarification on how this is used exactly??

sweepy84 avatar Sep 19 '18 07:09 sweepy84

@youtux @olegpidsadnyi Any insights on this? I am also having this issue.

iramisvalentincapco avatar May 21 '19 21:05 iramisvalentincapco

Scenarios is just a shortcut to collect all the scenarios from feature file (s) and turn them into the test items. So you can include them in your test_*.py files and organize your structure as you want.

olegpidsadnyi avatar May 21 '19 21:05 olegpidsadnyi

You may want to read the pytest best practices https://docs.pytest.org/en/latest/goodpractices.html

olegpidsadnyi avatar May 21 '19 21:05 olegpidsadnyi

So I have a few questions.

  1. If all the scenarios are collected, do they automatically map to the test_*.py files? a. If they are automatically mapped, how do they know which test_*.py files to map to?
  2. Does the scenarios() shortcut generate pre-mapped test_*.py automatically or do we need to pre-make these files?
  3. Should I put my 'features' and 'step_defs' directories outside of my 'Tests' directory?

iramisvalentincapco avatar May 21 '19 21:05 iramisvalentincapco

  1. You map scenarios from your test files. This is concios decision so that you can even include the same feature file in the totally different place and setup.
  2. Scenarios shortcut generates test_* functions inside the test module where it is used. There's a naming convention of lowercased underscored scenario name to make it a pythonic function. You can run a specific test if you specify it in the command line.
  3. You can keep your features apart, so that non technical person can browse them without reading python. There's a setting where your feature base folder is. But steps are pytest fixtures, that is what making them global and overridable. They should be part of the test folder. Usually all of them star-importes in the contest.py

olegpidsadnyi avatar May 21 '19 21:05 olegpidsadnyi

OK I think I understand. Another question I have is: Are feature file names such as 1_SOME_FEATURE-22.feature allowed?

iramisvalentincapco avatar May 21 '19 21:05 iramisvalentincapco

I hope so. You can include a specific scenario from the feature file, that you specify yourself. Or all the scenarios from the specified feature file. And (if I'm not mistaken) all the scenarios from .feature files of a specified folder recursively.

olegpidsadnyi avatar May 21 '19 21:05 olegpidsadnyi

So in my particular folder structure I have the following:

Tests
     features
          mobile
              login
                   1_login-2.feature
          web
              login
                   1_login-2.feature
     step_defs
          mobile
              login
                   login_step_fixtures.py
              conftest.py
          web
              login
                   login_step_fixtures.py
              conftest.py
conftest.py (root conftest)

Where my steps are implemented within <feature>_steps*.py. If I set scenarios('Tests/features') in my root conftest.py, will it automatically generate the test functions in the conftest.py and map them to the correct scenarios + steps?

iramisvalentincapco avatar May 21 '19 22:05 iramisvalentincapco

I think you need to create a test_X.py file(or files) on the same level as your step_defs and include the scenarios there.

olegpidsadnyi avatar May 22 '19 11:05 olegpidsadnyi

Hmmm interesting. Would that require decorating each def test_X(): with @scenario()?

It seems like the small amount of documentation on the scenarios shortcut implies that using it removes the need for decorating each individual test.

iramisvalentincapco avatar May 22 '19 12:05 iramisvalentincapco

You can use one test file as an entry point:

test_my_project.py

scenarios()

I'm often creating a test file per .feature file for the fine-grain control. If you want run a specific item you need to know the name of it. Which you can if you run it with collect only option. Some people still define a pythonic symbol per scenario by hand so you see all the definitions in the source code.

olegpidsadnyi avatar May 22 '19 12:05 olegpidsadnyi

AH, interesting! So I assume you have multiple test_*() function definitions inside of your test_my_project.py?

I've been finding difficulties with manually mapping each individual test function with the correct feature + scenario because the number of tests has recently grown significantly and will continue to do so.

Another issue I seem to be facing is with steps being collected or mapped incorrectly. For example the web/ and mobile/ directories both contain a login_step_defs.py which is imported to their respective conftest.py files. However running a test that uses the same step name(s) will currently collect the first version it finds, even though the actual step definition implementations may be different.

iramisvalentincapco avatar May 22 '19 13:05 iramisvalentincapco

Yes, please read https://docs.pytest.org/en/latest/goodpractices.html where it explains the scope of the fixture. You define steps somewhere globally and include them in your root level conftest.py

If you need to override a fixture (a step) you define or import it on the namespace below that. The current namespace level is the level of your test item.

olegpidsadnyi avatar May 22 '19 13:05 olegpidsadnyi

Ok, thank you for the documentation! It's leading me down a trail of more documentation but I think I have a better picture of how things are currently being collected and mapped.

I'd like to inquire as to what your current project structure looks like just so I can compare and contrast my findings with a different structure, if you would be willing. The main thing I am now wondering is do you use nested conftest.py files within subdirectories and do you include __init__.py files in every directory?

iramisvalentincapco avatar May 22 '19 13:05 iramisvalentincapco