-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to automatically collect *.feature files #342
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #342 +/- ##
==========================================
+ Coverage 95.80% 95.87% +0.06%
==========================================
Files 57 59 +2
Lines 2217 2253 +36
Branches 185 187 +2
==========================================
+ Hits 2124 2160 +36
Misses 62 62
Partials 31 31 ☔ View full report in Codecov by Sentry. |
Hi @youtux This PR is opened since December 2019. |
I'm sorry for the long wait before my reaction. # tests/test_all.py
from pytest_bdd import scenarios
scenarios('features') # this will traverse subdirectories as well This is documented in https://github.com/pytest-dev/pytest-bdd#scenarios-shortcut |
0bbddff
to
6a80101
Compare
Autocollecting of The main feature of this PR is that you can work with pytest temp.feature Another example. For instance, we have such project structure:
If we add from pytest_bdd import scenarios
scenarios('features') When we perform collecting of tests we will have such output:
We can see that all tests located at one file. In case when
We used this feature with auto collecting at our project and I can say that this cool feature. It simplifies working with We had a project structure something like this:
In local |
@youtux can you rerun Travis CI builds? It looks like there some problems with codecov reports uploading, I have no rights to do that. |
I see, it makes sense. @olegpidsadnyi do you agree on this? |
@youtux I'm not sure about this mapping of the text files to py files. When we started on pytest-bdd that was what made it different from the others. Especially when the same feature file could be included by 2 or more scenarios or parametrized according to a different backend or environment. |
It's up to end-user to choose how they want to organize their project. The idea is to provide more user-friendly API for I can create separate pytest plugin with this functionality but I thought it will be a great idea to add it to It up to you decide will this feature be included to |
That will still be possible of course, but I think it's a more "advanced" usage that many people just don't need. I think the two ways can co-exist, and I think we won't even need a flag to switch mode.
running
Of course this would require a major version bump, as users that had their |
@uriyyo the entry point to pytest tests is test_X.py files. The advantage of this is respecting the pytest environment. Since the test_X.py file has a place in the folder structure all the conftests and fixtures are respected. |
@uriyyo pytest-bdd is pytest in the first place. There's no goal to compete with behave at all |
@uriyyo On the other hand if .feature files are underneath the tests dir we could build a support to collect them as the doc suggesting https://docs.pytest.org/en/latest/example/nonpython.html I don't like modality much. So if we let .feature files to be collected then I'd prefer the major release that is not compatible (to avoid the setting). And that would probably mean we need to cleanup the tests of the pytest-bdd itself. |
@olegpidsadnyi This PR was implement using docs you mentioned above 😃 @olegpidsadnyi @youtux It looks like it will be big changes to a project, what we should do next? |
I can think of some step that have to be satisfied in order to have this feature:
|
I don't mind if we are making a breaking change. But of course then we need to rewrite the doc a bit. |
I will update this PR in order to meet all requirements mentioned by @youtux But the last question is, should we support registration of feature files using |
@uriyyo @olegpidsadnyi this is a very nice feature, would be glad to see it merging some time! |
I'm still trying to figure out how to make it possible to override fixture (in the new approach) for a specific .feature file; overriding the fixture on the conftest.pty would impact other feature files within the same directory, which is not desired. Option 1One possibility is that feature files that need specific pytest overrides (fixtures, marks, hooks e.g. pytest_collection_modifyitems) can be placed under their own directory and have a conftest.py that does it for them:
Option 2An alternative is to provide also a test_*.py whose name matches the feature file, and where we can put custom fixtures and hooks:
I'm not sure though how feasible and easy to implement is this second option. Maybe we can go with Option 1 at first, since it shouldn't require any implementation, and add the Option 2 in the future if we deem it necessary. |
I think option 1 will be perfect now. I will update PR implementation to match what you describe. |
""" | ||
Automatically collect *.feature files and create test modules for them. | ||
""" | ||
if CONFIG_STACK[-1].getini("bdd_auto_collect") and path.ext == ".feature": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to support line number(s) too?
# run scenario defined on line 2
$ pytest features/foo.feature:2
# run scenarios defined on line 2 and 12
$ pytest features/foo.feature:2:12
Thanks, but I think I will want to eventually do a big rewrite to make it behave this way, so don't it's quite possible this PR wont be merged |
Let me know, if you need help) |
Feature files could be stored in a separate folder tree. Symlinks could be used (since git 2.32) to place them near appropriate conftest.py files. So if some fixture needs to be overloaded - a separate conftest.py should be created in a nested folder (usual pytest way to overload fixtures). Please check the proposed project layout https://github.com/elchupanebrej/pytest-bdd-ng#features-autoload . |
Is there an alternative to this? I would really like to use this feature. I thought auto collection is the main advantage of behave, but if we have this in pytest-bdd this would be a giant leap. |
I currently using this awesome library. And I want to say thank you.
But I was confused when I started to work with
pytest-bdd
.It is unclear that you should register your
*.feature
file to the test script or use thescenarios
function.I think many of us want to have something like at
behave
library: you have*.feature
files and steps definition and that all, no registration of*.feature
files.In this pull request, I want to add such feature to
pytest-bdd
.In order to enable the auto collection of
*.feature
you should addbdd_auto_collect
option topytest.ini
:In case if
bdd_auto_collect
enabled pytest will collect*.feature
files usingpytest_collect_file
hook and create python module for them. Basically, it will create dummy python modules and fill them with features using thescenarios
function.Currently, this feature not compatible with manual registration of
*.feature
files usingscenario
orscenarios
. It will duplicate tests and run them twice.