Here is my test directory:
test/
unit/
some test files
conftest.py
acceptance/
some test files
conftest.py
Right now there are two conftest files for fixtures. Can I combine them into one conftest.py
file? If yes, where should I keep that common conftest.py
?
conftest.py : sharing fixtures across multiple filesYou can have multiple nested directories/packages containing your tests, and each directory can have its own conftest.py with its own fixtures, adding on to the ones provided by the conftest.py files in parent directories.
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.
conftest.py : sharing fixtures across multiple filesThe 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.py is where you setup test configurations and store the testcases that are used by test functions. The configurations and the testcases are called fixture in pytest.
You can combine the fixtures into a conftest.py
file within the top-level test
directory. Per this documentation, conftest files are per directory. The conftest file (and fixtures and plugins therein) should be available to test files in that directory and in child directories. If you do that, I recommend making your test
directory into a python package by adding a __init__.py
file (per the note in this documentation).
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