Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use single conftest.py for different test directories?

Tags:

pytest

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?

like image 736
Rajib Mitra Avatar asked Aug 15 '17 11:08

Rajib Mitra


People also ask

Can you have multiple Conftest files?

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.

Where should Conftest py be located?

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.

How does Conftest work in pytest?

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).

What is the use of Conftest in Python?

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.


1 Answers

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).

like image 121
Frank T Avatar answered Sep 24 '22 23:09

Frank T