-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Allow having multiple conftest.py on same level #3582
Comments
Hi @ikonst, For your use case, where you want fixtures in separate files just as a means to keep things more manageable, you can import the fixtures normally into the # conftest.py
from ._app_fixture import app
from ._regression_fixture import regression And so on. Importing fixtures into test files in general is not recommended as it might lead to subtle problems, but importing them into the |
Would that force me to import fixtures one by one, though? e.g. let's say I have 100 fixtures: 50 are data model fixtures (that I'd like to store in "conftest_data_models.py") and 50 are network call fixtures ("conftest_network_calls.py"). |
No, you can import them using |
@ikonst note that due to bugs pytest_plugins in conftests also breaks strangely |
I wonder if it would be feasible or possible already to have a module "conftest"? |
Currently we look explicitly for Lines 367 to 370 in 4d0297b
As to change this to support packages, it is possible I guess, but it would mean to change other places that also look for Lines 354 to 355 in 4d0297b
pytest/src/_pytest/fixtures.py Lines 1054 to 1057 in 4d0297b
Because of the possibility of introducing more bugs, I'm not sure it is worth trying to add support for this given that the "workaround" is pretty simple: import your symbols into the |
im -1 on this |
Yep, I agree with @RonnyPfannschmidt here. @ikonst did importing the fixtures into the |
Thanks for all your help. I'll test this on my codebase and report back. |
Thanks! |
|
Great. |
To follow up, my approach to import all fixtures under from pkgutil import walk_packages
from . import fixtures
for package in walk_packages(fixtures.__path__, prefix=fixtures.__name__ + '.'):
module = package.module_finder.find_module(package.name).load_module()
globals().update({k: v for k, v in vars(module).items() if not k.startswith('_')}) Having to copy-paste this code snippet across multiple conftest.py files seems suboptimal, but so does implicitly specifying all fixtures, e.g.
and the Furthermore, I didn't find a way to write a pytest plugin to hook into loading of conftest.py files, in order to inject this functionality. |
@nicoddemus any suggestions on how to achieve this when using the recommended Any suggestions are welcome here! :) |
For reference, I opted for using the pythonpath option to point to a separate directory containing helpers/utilities for the test suite. As an example,
The final project tree looks something like this for me:
|
The Since the star import only considers non-underscore items, this workaround does not feel great to me. I have to either do explicit imports or modify I think that automatic fixture imports only working from test files or |
@danielschenk It sounds like you just want to use |
We will add sibling test folders in the near future which should be (largely) independent, so that is not going to work for me. |
In our project, the conftest.py files have grown so large that we felt the need to refactor them into smaller modules. However, modules imported by conftest.py using
import
cannot contribute fixtures.We've tried using
pytest_plugins
but unfortunately (as the documentation says and as my testing confirms), importing a plugin with fixtures in one conftest.py makes them available in tests in parent and sibling hierarchies.I'd like to propose a simple solution, to have pytest look for
conftest.py
and thenconftest_*.py
(under the same directory). This way, unruly conftest.py files could be easily split up.(Let me know if it's an acceptable solution, I can implement it.)
The text was updated successfully, but these errors were encountered: