What is conftest.py in PyTest?

HOME

import pytest


@pytest.fixture
def shared_fixture():
    print("This will be executed before any test")

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

def test_fixture(shared_fixture):
    print("This is the fixture main method for demo2 class")
    print("Good Evening")

pytest test_demo1.py test_demo2.py -s

import pytest


@pytest.fixture
def shared_fixture():
    print("This will be executed before any test")
    yield
    print("This will be executed after any test")

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

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

pytest -s

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

PyTest Framework

HOME