pytest-loop
raw JSON → 1.0.13 verified Mon Apr 27 auth: no python
A pytest plugin that allows running tests in a loop until they fail (or pass), useful for detecting flaky tests. Current version 1.0.13, compatible with Python >=3.7. Maintenance mode with infrequent releases.
pip install pytest-loop Common errors
error AttributeError: module 'pytest' has no attribute 'mark' ↓
cause Trying to import pytest.mark.loop after patching pytest.mark incorrectly.
fix
Do not import pytest.mark.loop. Just use @pytest.mark.loop(N) in test files; the marker is registered automatically when pytest-loop is installed.
error Loop count not respected, test runs only once ↓
cause Forgetting to include @pytest.mark.loop or --loop option.
fix
Add --loop N to pytest command or decorate test with @pytest.mark.loop(N).
Warnings
gotcha The --loop option applies to all tests in the session. Use @pytest.mark.loop to control per-test loop count. ↓
fix Use pytest.mark.loop(N) for specific tests, or --loop N for global loop count.
gotcha Running in loop mode can cause side effects like repeated database writes or file modifications. Use with caution on tests with side effects. ↓
fix Ensure tests are idempotent or use fixtures that clean up after each iteration.
Imports
- pytest.mark.loop
pytest.mark.loop decorator (no import needed; automatically registered as a marker)
Quickstart
# test_example.py
import pytest
def test_flaky():
import random
assert random.choice([True, False])
# Run with: pytest --loop 10 test_example.py
# Or use @pytest.mark.loop(10) on a single test