Search code examples
sleeppytestintervals

Interval between tests in pytest


Is there a common practice to add interval between tests in pytest? Currently integration tests fail but work fine if running the tests individually.


Solution

  • You can use autouse fixtures in pytest to automatically sleep in between test cases:

    @pytest.fixture(autouse=True)
    def slow_down_tests():
        yield
        time.sleep(1)
    

    This fixture will automatically be used for all test cases and will yield execution to a test case so it can run normally, but when the test finishes, the execution will come back to this fixture and the sleep will be run.