In this tutorial, we are discussing the use of conftest.py in PyTest.
What is conftest.py in PyTest?
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). To know more about the fixtures in PyTest, please refer to this tutorial – What is Fixture in PyTest?
We can have multiple nested directories/packages containing the 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.

Below is the sample code for conftest.py.
import pytest
@pytest.fixture
def shared_fixture():
print("This will be executed before any test")
In test_demo1.py and test_demo2.py, we have used the shared fixture:
test_demo1.py
def test_fixture1(shared_fixture):
print("This is the fixture main method1 for demo1 class")
print("Good Morning")
def test_fixture2(shared_fixture):
print("This is the fixture main method2 for demo1 class")
print("Good AfterNoon")
test_demo2.py
def test_fixture(shared_fixture):
print("This is the fixture main method for demo2 class")
print("Good Evening")
Execute both the files using the below command:
pytest test_demo1.py test_demo2.py -s
The output of the above program is

In the above file – test_demo1.py, we have 2 methods and shared_fixture is called in both the methods. If we don’t want to pass shared_fixture multiple times. We can define a class and assign the fixture method to that class – @pytest.mark.usefixtures(“shared_fixture”). In that case, we don’t need to assign a fixture method to each and every method present in the class.
conftest.py
import pytest
@pytest.fixture
def shared_fixture():
print("This will be executed before any test")
yield
print("This will be executed after any test")
TestExample1.py
import pytest
@pytest.mark.usefixtures("shared_fixture")
class TestExample1:
def test_fixture1(self):
print("This is the fixture main method1 for demo1 class")
print("Good Morning")
def test_fixture2(self):
print("This is the fixture main method2 for demo1 class")
print("Good AfterNoon")
TestExample2.py
import pytest
@pytest.mark.usefixtures("shared_fixture")
class TestExample2:
def test_fixture(self):
print("This is the fixture main method for demo2 class")
print("Good Evening")
Execute both the files using the below command:
pytest -s
The output of the above program is

In the above example, we can see that before and after statement is printed for each test.
Suppose we want to run the fixture method once before the start and once at the end of the class. We need to add the below command:
import pytest
@pytest.fixture(scope="class")
def shared_fixture():
print("This will be executed before any test")
yield
print("This will be executed after any test")
The output of the above program is

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
