Skip to main content

Pytest plugin implementing flaky test failure detection and classification.

Project description

Pytest FlakeFighters

Project Status: Active – The project has reached a stable, usable state and is being actively developed. PyPI version Python versions Test status codecov Documentation Status GitHub License

Pytest plugin implementing flaky test failure detection and classification.

Read more about flaky tests here.

Features

  • Implements the DeFlaker algorithm for pytest
  • Implements two traceback-matching classifiers from Alshammari et al. (2024).
  • Implements a novel coverage-independence classifier that classifies tests as flaky if they fail independently of passing test cases that exercise overlapping code.
  • Optionally rerun or suppress flaky failures
  • Output results to JSON, HTML, or JUnitXML
  • Save test outcome history to a remote or local database

Comparison with Other Plugins

Flakefighters is a pytest plugin developed as part of the TestFLARE project. The plugin provides a "Swiss army knife" of techniques (called flakefighters) to detect flaky tests. Where existing flaky test plugins such as pytest-rerunfailures and pytest-flaky are primarily focused on rerunning (potentially) flaky tests until they pass, our main aim is to identify flaky tests by classifying test failures as genuine or flaky. The pytest-flakefinder plugin does this by simply rerunning tests multiple times and observing the result.

By contrast, Flakefighters incorporates several cutting edge flaky test detection techniques from research to automatically classify test failures as either genuine: indicating either a fault in the code or a mis-specified test case, or flaky: indicating a test with a nondeterministic outcome. Flaky tests are then reported separately in the test report, and can be optionally rerun or suppressed so they don't block CI/CD pipelines.

Feature pytest-flakefighters pytest-rerunfailures pytest-flaky pytest-flakefinder pytest-replay
Purpose Classify test failures as genuine or flaky Rerun failing tests in case they are flaky Decorator-based reruns Copy tests to observe nondeterministic outcomes Reproduce flaky failures from CI when running with xdist
Detection Method DeFlaker algorithm + coverage analysis None None Reruns None
Reporting Terminal, HTML, JSON, JUnitXML Terminal Terminal Terminal Terminal
History Tracking Database of test outcomes over commits None None None None
Rerun Option Optional Required Required Required Required
Suppression Option Optional None None None None
Debugging support Insight into why tests are flaky None None None Reliable reproduction of flaky failures

When to Use pytest-flakefighters

Use pytest-flakefighters when you want to:

  • Understand WHY tests are flaky, not just hide the symptoms
  • Classify flaky tests by root cause (coverage-independent, traceback-matched, etc.)
  • Track test flakiness over time and across commits
  • Make informed decisions about whether failures are legitimate

When to use alternatives

Can They Work Together?

Yes! pytest-flakefighters can be combined with other flaky test plugins:


For more information on flaky test management best practices, see the pytest documentation.

Installation

With pip

You can install the extension by running pip install pytest-flakefighters from within your project's virtual environment.

With uv

If you use uv for Python package management, you can install pytest-flakefighters with uv add pytest-flakefighters. This will add the plugin to your main dependencies.

dependencies = [
    "pytest-flakefighters>=x.y.z",
]

However, pytest is typically a development dependency, and so should be added with uv add --dev pytest-flakefighters.

[dependency-groups]
dev = [
    "pytest-flakefighters>=x.y.z",
]

From source (for development)

You can install "pytest-flakefighters" by cloning this repo and running pip install . from the root directory. If you intend to develop the plugin, run pip install -e .[dev] instead.

If you use uv, you can install pytest-flakefighters with:

# Install with uv
uv pip install .

# For development
uv pip install -e .[dev]

Usage

FlakeFighter is intended to run on git repositories that have test suites runnable with pytest. Once you have installed FlakeFighter, you can run it from the root directory of your repo simply by running pytest in your usual way. FlakeFighter has the following arguments.

  --target-commit=TARGET_COMMIT
                        The target (newer) commit hash. Defaults to HEAD (the most recent commit).
  --source-commit=SOURCE_COMMIT
                        The source (older) commit hash. Defaults to HEAD^ (the previous commit to target).
  --repo=REPO_ROOT      The commit hash to compare against.
  --suppress-flaky-failures-exit-code
                        Return OK exit code if the only failures are flaky failures.
  --no-save             Do not save this run to the database of previous flakefighters runs.
  -M LOAD_MAX_RUNS, --load-max-runs=LOAD_MAX_RUNS
                        The maximum number of previous runs to consider.
  -D DATABASE_URL, --database-url=DATABASE_URL
                        The database URL. Defaults to 'flakefighter.db' in current working directory.
  --store-max-runs=STORE_MAX_RUNS
                        The maximum number of previous flakefighters runs to store. Default is to store all.
  --time-immemorial=TIME_IMMEMORIAL
                        How long to store flakefighters runs for, specified as `days:hours:minutes`. E.g. to store
                        tests for one week, use 7:0:0.

Enabling/Disabling the Plugin

To enable the plugin, run pytest with the --flakefighters argument

pytest --flakefighters

You can also configure this in your pyproject.toml:

[tool.pytest.ini_options]
addopts = "--flakefighters"

Configuration

By default, the plugin will only use the DeFlaker algorithm to classify flaky tests. If you would like to use other algorithms as well (or instead), you need to configure these. This can be done by adding appropriate fields in your pyproject.toml or pytest.ini file. For example, you could add the following to your pyproject.toml.

[tool.pytest.ini_options.pytest_flakefighters.flakefighters.deflaker.DeFlaker]
run_live=true # run the classifier immediately after each test

[tool.pytest.ini_options.pytest_flakefighters.flakefighters.traceback_matching.TracebackMatching]
run_live=false # run the classifier at the end of the test suite

[tool.pytest.ini_options.pytest_flakefighters.flakefighters.traceback_matching.CosineSimilarity]
run_live=false # run the classifier at the end of the test suite
threshold=0.8 # Cosine similarity >= 0.8 is classed as a match

[tool.pytest.ini_options.pytest_flakefighters.flakefighters.coverage_independence.CoverageIndependence]
run_live=false # run the classifier at the end of the test suite
threshold=0.1 # Distance <= 0.1 is classed as "similar"
metric=hamming # Use Hamming distance
linkage_method=complete # Use complete linkage for clustering

[!NOTE] The above configuration is just an example meant to demonstrate the various parameters that can be supplied, and is not a recommendation or "default". You should choose the parameter values that are appropriate for your project, especially threshold values for CosineSimilarity and CoverageIndependence.

Further details can be found in the configuration documentation.

Contributing

Contributions are very welcome. Tests can be run with pytest, please ensure the coverage at least stays the same before you submit a pull request.

Flake Fighters

Our plugin is made up of a collection of heuristics that come together to help inform whether a test failure is genuine or flaky. These come in two "flavours": those which run live after each test, and those which run at the end of the entire test suite. Both extend the base class FlakeFighter and implement the flaky_failure method, which returns True if the test is deemed to be flaky.

Issues

If you encounter any problems, please file an issue along with a detailed description.


This pytest plugin was generated with Cookiecutter along with @hackebrot's cookiecutter-pytest-plugin template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_flakefighters-0.6.0.tar.gz (211.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_flakefighters-0.6.0-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest_flakefighters-0.6.0.tar.gz.

File metadata

  • Download URL: pytest_flakefighters-0.6.0.tar.gz
  • Upload date:
  • Size: 211.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_flakefighters-0.6.0.tar.gz
Algorithm Hash digest
SHA256 9dd0509deb1ac5f84c5a96fc25596d717224300d120e4ca7c074c9a101289c41
MD5 7bf8365f0ee67f50d73e9fe32374bb23
BLAKE2b-256 9d6b5ceba24365d51a8842e68cc2fb93a5f5169217a3d2ae2de1c2466e1c6fa2

See more details on using hashes here.

File details

Details for the file pytest_flakefighters-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_flakefighters-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f4d45d2d052a20747814620568212c82f8fb1a624bc7a6bfefc63328bd579d1
MD5 92cf2e624638b3e7e4afafcee006b25e
BLAKE2b-256 fe92437636f0e5ce7bcd7b7ee96e684ae4dfa71eacbd3fc0e065d9eb9eb316ea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page