Registry / testing / pytest-repeat

pytest-repeat

JSON →
library0.9.4pypypiunverified

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-repeat
INSTALL
IMPORT
SIG · PYTEST-REPEAT
P
pytest-repeat
testingpythonv0.9.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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.

import pytest # test_example.py counter = 0 @pytest.mark.repeat(count=2) def test_something_flaky(): global counter counter += 1 print(f"\nRunning test_something_flaky, repeat count: {counter}") assert counter < 3 # This will pass on first repeat, fail on second if not reset def test_another_one(): assert True # To run via CLI: # pytest --count=3 test_example.py -v # (This would repeat both tests 3 times) # # To run with marker (as above): # pytest test_example.py -v
pytest --version
Debug
Known issues
gotchaFixture teardown and setup behavior with `repeat-scope` can be counter-intuitive. By default (`--repeat-scope=function`), all fixtures including 'function' scoped ones are torn down and re-setup for each test repeat. However, if you set `--repeat-scope` to `module` or `session`, fixtures scoped at that level (or higher) will only be setup once for the entire block of repeats within that scope, not per individual repeat. This can lead to state leakage if tests or higher-scoped fixtures modify shared resources that are not properly reset between repeats.
fix
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.
affects: All versions
gotchaTests that are not idempotent and modify global state or mutable shared objects can produce inconsistent results when repeated. Each repeat runs within the same Python process and memory space (unless `pytest-xdist` is used), meaning side effects from previous repeats can affect subsequent ones.
fix
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.
affects: All versions
gotchaThe default `repeat-scope` is `function`. This means if you use `--count=N` without explicitly setting `--repeat-scope`, *every* test item (including setup/teardown for `function`-scoped fixtures) will be repeated `N` times independently. This might not be the desired behavior if you want to repeat an entire module or session as a block without re-running module/session scoped fixtures repeatedly.
fix
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.
affects: All versions
Errors
Common errors & fixes
pytest: error: unrecognized arguments: --repeat 5
The command-line option for repeating tests in pytest-repeat is `--count`, not `--repeat`.
fix
pytest --count 5
NameError: name 'repeat' is not defined
The `@repeat` marker must be accessed through the `pytest.mark` namespace, i.e., `@pytest.mark.repeat(count=N)`.
fix
@pytest.mark.repeat(count=5)
def test_something():
    pass
bash: pytest-repeat: command not found
`pytest-repeat` is a plugin for `pytest`, not a standalone executable; it extends the `pytest` command-line interface.
fix
pytest --count 5
pytest: error: argument --count: invalid int value: 'five'
The `--count` command-line option expects an integer value to specify how many times to repeat the tests.
fix
pytest --count 5
Upgrade
Version history
0.9.4latest on PyPI · released Apr 7, 2025
Audit
Dependencies
pytestrequiredCore dependency for a pytest plugin.
Agent activity
5 hits · last 30 days
node
4
Resources
pytest-repeat — pip install pytest-repeat · libregistry