pytest-bdd
pytest-bdd copied to clipboard
Where the scenarios shortcut should be place?
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.
yes likewise - the documentation is very unclear, and provides no example. can someone please provide some clarification on how this is used exactly??
@youtux @olegpidsadnyi Any insights on this? I am also having this issue.
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.
You may want to read the pytest best practices https://docs.pytest.org/en/latest/goodpractices.html
So I have a few questions.
- If all the scenarios are collected, do they automatically map to the
test_*.pyfiles? a. If they are automatically mapped, how do they know whichtest_*.pyfiles to map to? - Does the scenarios() shortcut generate pre-mapped
test_*.pyautomatically or do we need to pre-make these files? - Should I put my 'features' and 'step_defs' directories outside of my 'Tests' directory?
- 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.
- 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.
- 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
OK I think I understand. Another question I have is: Are feature file names such as 1_SOME_FEATURE-22.feature allowed?
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.
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?
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.
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.
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.
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.
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.
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?