pytest-flakefinder
pytest-flakefinder is a plugin for the pytest testing framework that helps identify flaky tests by running them multiple times within a single pytest invocation. The current version, 1.1.0, was last released in October 2022 and is maintained by Dropbox, offering features to configure the number of runs and set a maximum execution time.
Warnings
- gotcha Running tests many times with `--flake-runs` without parallelization (e.g., using `pytest-xdist`) can significantly increase execution time.
- gotcha The `--flake-max-minutes` option will only skip tests that *start* after the timeout has been reached. It does not interrupt currently running tests.
- gotcha pytest-flakefinder is designed to *find* flakiness, not to *fix* or *mitigate* it during regular CI runs. Its purpose is diagnostic.
Install
-
pip install pytest-flakefinder
Quickstart
import random
def test_might_be_flaky():
# This test has a 30% chance of failing to simulate flakiness
if random.random() < 0.3:
assert False, "This test failed flakily!"
else:
assert True
# To run all tests 100 times to find flakes:
# pytest --flake-finder --flake-runs=100
# To run tests with a maximum time limit of 5 minutes:
# pytest --flake-finder --flake-max-minutes=5
# Combine with pytest-xdist for parallel execution (requires prior installation of pytest-xdist):
# pytest -n auto --flake-finder --flake-runs=100