-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Session scoped fixture is called twice #3217
Comments
Hi! You don't need to import fixtures You can use fixtures in test files like
or you can add your fixture file to conftest as plugin:
conftest.py
test_one.py
test_two.py
result:
if you have different files with your fixtures you can add it in your conftest like
but please note this in future:
|
Hi, Thanks for the info, I knew you could import fixtures in the conftest but I didn't know you could do it as a plugin. However, do you mean that I must not import fixtures like that? |
I might be wrong, but I guess:
files:
test_one.py:
test_two.py:
test_three.py:
start tests by command
FixtureDef objects:
Output and results:
I don't think that it's a bug, but you can ask more core team (@nicoddemus , @The-Compiler , @RonnyPfannschmidt ) for details or check this code: Lines 1016 to 1044 in e7bcc85
|
bascially - fixtures come from plugins, or the test module - if each module import the fixture, then pytest considers each imported fixture as own separate fixture of the test module so pytest basically sees 2 fixtures with the same name, one in the one test module, the other in the other test module the most simple way to consolidate is a conftest, followed by having a pytest plugin |
Okay thanks ! It seemed strange to me because - correct me if I'm wrong - importing the same function in two different files actually gives the exact same object in memory. I will import all my session scoped fixtures in the conftest then. |
correct, thats a good solution 👍 in general we consider it good practice put fixtures that are shared across modules/folders into conftests for consistency i closing this issue as the problem seems to be solved, if you feel there is more to solve please dont hesitate to call out |
May the force be with you, folks! I've smashed helluva day to understand what the heck is happening with those scopes! |
When setting up the fixture list within a class, we were doing something along the lines of: ``` class TestClass: UNIT = {"cluster": [local_cluster]} ``` This requires an import of `local_cluster` from conftest. Turns out, this causes session scoped fixtures to be recomputed (pytest-dev/pytest#3217). It's bad practice to import from conftest in the first place. So, I changed the fixtures that run `getfixturevalue` to just use the string argument, and we can avoid importing, and rely on pytest to set up the fixtures correctly. Now we set up fixtures like so: ``` class TestClass: UNIT = {"cluster": ["local_cluster"]} ``` Moreover... if a fixture is defined in a nested `conftest.py`, and imported in the global `conftest.py`, then there can be conflicts. The way tests work is that they'll look heirarchically upward in conftest.py files, and then grab the first fixture they find that matches the name. Even if the global `conftest.py` imports from a nested `conftest.py` file, it is considered a different fixture object (unintuitive, yes). This will sometimes lead to session scoped fixtures being initialized twice in the same session. More info on nested conftest.py files [here](pytest-dev/pytest#1931).
Hello,
Version: I'm running Ubuntu 14.04 with pytest 3.4.0.
Use case: I am using a session scoped fixture which I import and use in two different test files.
Issue: The fixture is run one time for each file when I expect it to be run only once for the whole session.
Example:
fixtures.py
test1.py
test2.py
Hello is printed twice instead of once.
Workaround: A workaround is to import it in the conftest but since I am using hundred of fixtures, I prefer not to import everything in the conftest.
The text was updated successfully, but these errors were encountered: