Skip to main content
http://opensource.box.com/badges/stable.svg https://github.com/box/flaky/actions/workflows/tox.yml/badge.svg?branch=master&event=push https://img.shields.io/pypi/v/flaky.svg

About

Flaky is a plugin for pytest that automatically reruns flaky tests.

Ideally, tests reliably pass or fail, but sometimes test fixtures must rely on components that aren’t 100% reliable. With flaky, instead of removing those tests or marking them to @skip, they can be automatically retried.

For more information about flaky, see this presentation.

Marking tests flaky

To mark a test as flaky, simply import flaky and decorate the test with @flaky:

from flaky import flaky
@flaky
def test_something_that_usually_passes(self):
    value_to_double = 21
    result = get_result_from_flaky_doubler(value_to_double)
    self.assertEqual(result, value_to_double * 2, 'Result doubled incorrectly.')

By default, flaky will retry a failing test once, but that behavior can be overridden by passing values to the flaky decorator. It accepts two parameters: max_runs, and min_passes; flaky will run tests up to max_runs times, until it has succeeded min_passes times. Once a test passes min_passes times, it’s considered a success; once it has been run max_runs times without passing min_passes times, it’s considered a failure.

@flaky(max_runs=3, min_passes=2)
def test_something_that_usually_passes(self):
    """This test must pass twice, and it can be run up to three times."""
    value_to_double = 21
    result = get_result_from_flaky_doubler(value_to_double)
    self.assertEqual(result, value_to_double * 2, 'Result doubled incorrectly.')

Marking a class flaky

In addition to marking a single test flaky, entire test cases can be marked flaky:

@flaky
class TestMultipliers(TestCase):
    def test_flaky_doubler(self):
        value_to_double = 21
        result = get_result_from_flaky_doubler(value_to_double)
        self.assertEqual(result, value_to_double * 2, 'Result doubled incorrectly.')

    @flaky(max_runs=3)
    def test_flaky_tripler(self):
        value_to_triple = 14
        result = get_result_from_flaky_tripler(value_to_triple)
        self.assertEqual(result, value_to_triple * 3, 'Result tripled incorrectly.')

The @flaky class decorator will mark test_flaky_doubler as flaky, but it won’t override the 3 max_runs for test_flaky_tripler (from the decorator on that test method).

Pytest marker

When using pytest, @pytest.mark.flaky can be used in place of @flaky.

Don’t rerun certain types of failures

Depending on your tests, some failures are obviously not due to flakiness. Instead of rerunning after those failures, you can specify a filter function that can tell flaky to fail the test right away.

def is_not_crash(err, *args):
    return not issubclass(err[0], ProductCrashedError)

@flaky
def test_something():
    raise ProductCrashedError

@flaky(rerun_filter=is_not_crash)
def test_something_else():
    raise ProductCrashedError

Flaky will run test_something twice, but will only run test_something_else once.

It can also be used to incur a delay between test retries:

import time

def delay_rerun(*args):
    time.sleep(1)
    return True

@flaky(rerun_filter=delay_rerun)
def test_something_else():
    ...

Activating the plugin

With pytest, flaky will automatically run. It can, however be disabled via the command line:

pytest -p no:flaky

Command line arguments

No Flaky Report

Pass --no-flaky-report to suppress the report at the end of the run detailing flaky test results.

Shorter Flaky Report

Pass --no-success-flaky-report to suppress information about successful flaky tests.

Force Flaky

Pass --force-flaky to treat all tests as flaky.

Pass --max-runs=MAX_RUNS and/or --min-passes=MIN_PASSES to control the behavior of flaky if --force-flaky is specified. Flaky decorators on individual tests will override these defaults.

Additional usage examples are in the code - see test/test_pytest/test_pytest_example.py

Installation

To install, simply:

pip install flaky

Compatibility

Flaky is tested with the following test runners and options:

  • Py.test. Works with pytest-xdist but not with the --boxed option. Doctests cannot be marked flaky.

Contributing

See CONTRIBUTING.rst.

Setup

Create a virtual environment and install packages -

mkvirtualenv flaky
pip install -r requirements-dev.txt

Testing

Run all tests using -

tox

The tox tests include code style checks via pycodestyle and pylint.

Release files for flaky 3.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flaky 3.8.1
File Size Uploaded
flaky-3.8.1.tar.gz 25.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flaky 3.8.1
File Interpreter ABI Platform
flaky-3.8.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 44.4 kB

Release files / flaky-3.8.1.tar.gz

Download URL flaky-3.8.1.tar.gz
Size 25.2 kB
Tags Source
SHA-256 checksum
How to use checksums
47204a81ec905f3d5acfbd61daeabcada8f9d4031616d9bcb0618461729699f5
BLAKE2b-256 checksum
How to use checksums
5bc5ef69119a01427204ff2db5fc8f98001087bcce719bbb94749dcd7b191365
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.4

Release files / flaky-3.8.1-py2.py3-none-any.whl

Download URL flaky-3.8.1-py2.py3-none-any.whl
Size 19.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
194ccf4f0d3a22b2de7130f4b62e45e977ac1b5ccad74d4d48f3005dcc38815e
BLAKE2b-256 checksum
How to use checksums
7fb8b830fc43663246c3f3dd1ae7dca4847b96ed992537e85311e27fa41ac40e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.4

Release history Release notifications | RSS feed

This release

3.8.1 This release

2 release files

3.8.0

2 release files

3.7.0

2 release files

3.6.1

2 release files

3.6.0

2 release files

3.5.3

2 release files

3.5.2

2 release files

3.5.1

2 release files

3.5.0

2 release files

3.4.0

2 release files

3.3.0

2 release files

3.2.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.3

2 release files

3.0.2

3.0.1

2 release files

3.0.0

2 release files

2.4.1

2 release files

2.4.0

2 release files

2.2.0

2 release files

2.1.2

1 release file

2.1.1

1 release file

2.1.0

1 release file

2.0.4

1 release file

2.0.3

1 release file

2.0.0

1 release file

1.1.0

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page