I've got a large conftest.py file that I wish to split into smaller parts, for two reasons:
I am not aware of any mechanism provided by pytest to resolve conftest files in multiple locations within the same folder, so I contrived one, below:
import sys
import os
sys.path.append(os.path.dirname(__file__))
from _conftest_private_part_1 import *
from _conftest_private_part_2 import *
from _conftest_private_part_3 import *
@pytest.fixture
def a_fixture_that_is_part_of_the_public_conftest_api():
pass
This works for my needs, but I do wonder if there is a better way.
You could also create more conftest files. A straightforward solution is to separate tests for example into unit/integrations/ui and keep conftest files there.
The conftest.py file serves as a means of providing fixtures for an entire directory. Fixtures defined in a conftest.py can be used by any test in that package without needing to import them (pytest will automatically discover them).
Conftest is a utility to help you write tests against structured configuration data. For instance, you could write tests for your Kubernetes configurations, Tekton pipeline definitions, Terraform code, Serverless configs or any other structured data.
You can put fixtures into individual test files, but to share fixtures among multiple test files, you need to use a conftest.py file somewhere centrally located for all of the tests. For the Tasks project, all of the fixtures will be in tasks_proj/tests/conftest.py. From there, the fixtures can be shared by any test.
Second, yes, you can have other conftest.py files in subdirectories of the top tests directory. If you do, fixtures defined in these lower-level conftest.py files will be available to tests in that directory and subdirectories.
The conftest.py file serves as a means of providing fixtures for an entire directory. Fixtures defined in a conftest.py can be used by any test in that package without needing to import them (pytest will automatically discover them).
The output of the above program is shown in the snapshot below: The data in the file is split into several lines and each line is returned as an element in the list by making use of a split function called the splitlines () function in Python.
Create a new file conftest.py and add the below code into it − Now, we have the files test_div_by_3_6.py and test_div_by_13.py making use of the fixture defined in conftest.py. The tests will look for fixture in the same file. As the fixture is not found in the file, it will check for fixture in conftest.py file.
You can put your stuff in other modules and reference them using a pytest_plugins
variable in your conftest.py
:
pytest_plugins = ['module1', 'module2']
This will also work if your conftest.py
has hooks on them.
You shouldn't need any fancy magic for that. py.test automatically adds the path of the current test file to sys.path
, as well as all parent paths up to the directory it was targeted at.
Because of that, you don't even need to put that shared code into a conftest.py
. You can just put into plain modules or packages and then import it (if you want to share fixtures, those have to be in a conftest.py
).
Also, there is this note about importing from conftest.py
in the documentation:
If you have conftest.py files which do not reside in a python package directory (i.e. one containing an
__init__.py
) then “import conftest
” can be ambiguous because there might be otherconftest.py
files as well on yourPYTHONPATH
orsys.path
. It is thus good practise for projects to either putconftest.py
under a package scope or to never import anything from aconftest.py
file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With