Pytest schedule intervals between groups of tests

Viewed 506

Is there any way of telling pytest to run a certain set of tests, then wait for a known amount of time, then run another set of tests? For example, if I have tests with the following requirements:

  • Each test has 3 parts (3 methods to execute)
    • Part 2 must not be run for each test until a specific, known amount of time has passed since running part 1.
    • Part 3 must not be run for each test until a specific, known amount of time has passed since running part 2.
  • If I stitched parts 1, 2 and 3 together for each test and just used time.sleep(), this would take far too long to execute all tests.
  • Instead I want to run all of the part 1s back to back, then wait a known amount of time, then run all of the part 2s back to back, then wait a known amount of time, then run all of the part 3s.

It appears that this should be possible to implement using markers https://docs.pytest.org/en/stable/example/markers.html and probably implementing hooks https://docs.pytest.org/en/latest/reference.html#hooks to implement certain behaviour based on the markers used, though I'm not very familiar with pytest hooks.

I also came across pytest-ordering https://pytest-ordering.readthedocs.io/en/develop/ which appears to provide behaviour close to what I'm looking for. I just need a way of waiting between certain groups of tests.

1 Answers

You could combine all part one tests in one class, all part two tests in another class, and use class scope fixture for the delay, something like this:

import pytest
import time


@pytest.fixture(scope='class')
def delay():
    time.sleep(5)


class TestPart1:
    def test_one_part_1(self):
        assert 1 == 1

    def test_two_part_1(self):
        assert 2 == 2


@pytest.mark.usefixtures("delay")
class TestPart2:
    def test_one_part_2(self):
        assert 1 == 1

    def test_two_part_2(self):
        assert 2 == 2
Related