pytest plugin for looping tests

pytest, loop, plugin, pytest-plugin, python, python3
pip install pytest-loop==1.0.6



pytest-loop is a plugin for pytest that makes it easy to loop a single test, or multiple tests, a specific number of times or for a certain duration of time. This plugin merges pytest-repeat and pytest-stress with a fix for test results.

License PyPI Python versions See Build Status on GitHub Actions Issues Requirements


You will need the following prerequisites in order to use pytest-loop: - Python 3.6+ or PyPy - pytest 6 or newer

Only tested with Pytest version 6.2.5 and 7.0.1.


To install pytest-loop:

$ pip install pytest-loop


Iterative Loop:

Use the --loop command line option to specify how many times you want your test, or tests, to be run:

$ pytest --loop=10

Each test collected by pytest will be run n times.

If you want to mark a test in your code to be looped a number of times, you can use the @pytest.mark.loop(n) decorator:

import pytest

def test_loop_decorator():

Time based loop:

Loop tests for 30 seconds:

$ pytest --seconds 30

Loop tests for 45 minutes:

$ pytest --minutes 45

Loop tests for 8 hours:

$ pytest --hours 8

Loop tests for 1 hour 8 minutes and 9 seconds:

$ pytest --hours 1 --minutes 8 --seconds 9

Need to wait some time after each test loop?:

$ pytest --delay 5 --hours 4 --minutes 30

You can also add these values to config files:

addopts = --hours 1 --minutes 30

Note: These loop times include setup and teardown operations as well. So if you have a test setup that takes 5 seconds, your actual tests will run for 5 seconds less than your desired time.

looping a test until failure:

If you are trying to diagnose an intermittent failure, it can be useful to run the same test over and over again until it fails. You can use pytest's -x option in conjunction with pytest-loop to force the test runner to stop at the first failure. For example:

$ pytest --loop=1000 -x

This will attempt to run 1000 times, but will stop as soon as a failure occurs.

$ pytest --hours 10 -x test_file.

This will attempt to run for 10 hours, but will stop as soon as a failure occurs.