I want to repeat the same pytest tests N times to measure execution speed or calculate average timing for different parameters.
I tried using the pytest_runtestloop hook like this:
def pytest_runtestloop(session):
repeat = int(session.config.option.repeat)
assert isinstance(repeat, int), "Repeat must be an integer"
for i in range(repeat):
session.config.pluginmanager.getplugin("main").pytest_runtestloop(session)
return True
However, the setup fixture with scope="class" only runs during the first iteration. For example:
class TestSomething:
@classmethod
@pytest.fixture(scope="class", autouse=True)
def setup(cls):
# setup function
def test_something(self):
# test function
Here, setup is called only the first cycle, but test_something runs both times if repeat=2.
What am I doing wrong, and is there a better approach to repeat tests while re-running fixtures each time?