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.
Requirements
You will need the following prerequisites in order to use pytest-loop: - Python 3.7+ or PyPy - pytest 7 or newer
Installation
To install pytest-loop:
$ pip install pytest-loop
Usage
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 test_file.py
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
@pytest.mark.loop(3)
def test_loop_decorator():
pass
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:
[pytest] 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 test_file.py
This will attempt to run test_file.py 1000 times, but will stop as soon as a failure occurs.
$ pytest --hours 10 -x test_file.
This will attempt to run test_file.py for 10 hours, but will stop as soon as a failure occurs.
UnitTest Style Tests
Unfortunately pytest-loop is not able to work with unittest.TestCase test classes.
These tests will simply always run once, regardless of --loop, and show a warning.
Resources
Metadata
Release files for pytest-loop 1.0.13
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytest_loop-1.0.13.tar.gz | 12.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_loop-1.0.13-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.9 kB
Release files / pytest_loop-1.0.13.tar.gz
| Download URL | pytest_loop-1.0.13.tar.gz |
|---|---|
| Size | 12.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
be396c0293c4a23e9c96b99f696ad0afd6ae5e4b23d4ca0724eda0660530e5a0
|
|
BLAKE2b-256 checksum How to use checksums |
31a6cacf098388cf5998970a5a5c9c8a298b91131ac6350cf372a57fc0c7cdda
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Release files / pytest_loop-1.0.13-py3-none-any.whl
| Download URL | pytest_loop-1.0.13-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8d902c801413c5a16ee70d618259161a38f9f188f9d7a986ccc557deabca4f84
|
|
BLAKE2b-256 checksum How to use checksums |
a2f2204ff93e2ab8232b70dd45fde3c6cad10ac6ab4298dc96ed2f0e9d52a288
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|