pytest-repeat is a pytest plugin that allows you to repeat tests a specified number of times, either globally via command-line options or per test item using markers. This can be useful for stress testing, identifying flaky tests, or observing behavior under repeated execution. The current version is 0.9.4, and it generally follows pytest's release cycle, with updates typically coinciding with or following major pytest releases.
pip install pytest-repeatNo compatibility data collected yet for this library.
To use pytest-repeat, simply install it. You can repeat all collected tests using the `--count` CLI option, or apply `pytest.mark.repeat(count=N)` to individual test functions or classes. The example demonstrates both a test marked for repetition and a global CLI usage comment. Note the `counter` variable to illustrate how state can persist or need careful management across repeats.
Be explicit about fixture scopes and `repeat-scope`. If state isolation is crucial between repeats, ensure `function`-scoped fixtures handle cleanup, or use `pytest.fixture(autouse=True)` with proper teardown logic if higher scopes are needed. Consider `pytest-xdist` for true isolation across parallel runs instead of repeats for certain scenarios.
Ensure tests are isolated. Use fixtures for setting up and tearing down state, making sure any modifications are reverted or unique for each test run (or repeat). Avoid global variables where possible, or reset them rigorously in `setup_function` / `teardown_function` or `autouse` fixtures.
If you intend to repeat an entire module or session as a block, use `--repeat-scope=module` or `--repeat-scope=session` respectively, in conjunction with `--count`. This ensures that higher-scoped fixtures are only run once per the chosen repeat scope.
pytest --count 5
@pytest.mark.repeat(count=5)
def test_something():
passpytest --count 5
pytest --count 5