pytest-repeat

0.9.4 · active · verified Thu Apr 09

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.

Warnings

Install

Quickstart

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

view raw JSON →