Reusing Fixtues By All Tests

Python
Author

Imad Dabbura

Published

December 28, 2022

There are some fixtures that we want to use them with every test function/method such as fixtures that report the execution time of each test. So instead of keep using the name of these fixtures in all tests, we can set autouse to True when defining the fixture so it will be called always by all tests in its scope.

@pytest.fixture(autouse=True)
def test_time():
    start = time.time()
    yield
    end = time.time()
    print(f"test duration: {end - start:0.3} seconds")