Skip to main content

Adds the ability to retry flaky tests in CI environments

Project description

Tests

pytest-retry

pytest-retry is a plugin for Pytest which adds the ability to retry flaky tests, thereby improving the consistency of the test suite results.

Requirements

pytest-retry is designed for the latest versions of Python and Pytest. Python 3.9+ and pytest 7.0.0 are required.

Installation

Use pip to install pytest-retry:

$ pip install pytest-retry

Usage

There are two main ways to use pytest-retry:

1. Command line

Run Pytest with the command line argument --retries in order to retry every test in the event of a failure. The following example will retry each failed up to two times before proceeding to the next test:

$ python -m pytest --retries 2

An optional delay can be specified using the --retry-delay argument. This will insert a fixed delay (in seconds) between each attempt when a test fails. This can be useful if the test failures are due to intermittent environment issues which clear up after a few seconds

$ python -m pytest --retries 2 --retry-delay 5

2. Pytest flaky mark

Mark individual tests as 'flaky' to retry them when they fail. If no command line arguments are passed, only the marked tests will be retried. The default values are 1 retry attempt with a 0-second delay

@pytest.mark.flaky
def test_unreliable_service():
    ...

The number of times each test will be retried and/or the delay can be manually specified as well

@pytest.mark.flaky(retries=3, delay=1)
def test_unreliable_service():
    ...

Finally, there is both a command line option and flaky mark argument for the test timing method, which can either be overwrite (default) or cumulative. With cumulative timing, the duration of each test attempt is summed for the overall test duration reported at the end. The default behavior simply uses the timing for the final attempt.

@pytest.mark.flaky(timing='overwrite')
def test_unreliable_service():
    ...

If you're not sure which to use, stick with the default overwrite method. This generally plays nicer with time-based test splitting algorithms and will result in more even splits.

The flaky mark will override any command line options if passed when running Pytest.

Things to consider

  • Currently, failing test fixtures are not retried. In the future, flaky test setup may be retried, although given the undesirability of flaky tests in general, flaky setup should be avoided at all costs. Any failures during teardown will immediately halt further attempts so that they can be addressed immediately. Make sure your teardowns always work reliably regardless of the number of retries when using this plugin

  • When a flaky test is retried, the plugin runs teardown steps for the test as if it had passed. This is to ensure that any partial state created by the test is cleaned up before the next attempt so that subsequent attempts do not conflict with one another. Class and module fixtures are included in this teardown with the assumption that false test failures should be a rare occurrence and the performance hit from re-running these potentially expensive fixtures is worth it to ensure clean initial test state. With feedback, the option to not re-run class and module fixtures may be added, but in general, these types of fixtures should be avoided for known flaky tests.

  • Flaky tests are not sustainable. This plugin is designed as an easy short-term solution while a permanent fix is implemented. Use the reports generated by this plugin to identify issues with the tests or testing environment and resolve them.

Reporting

pytest-retry intercepts the standard Pytest report flow in order to retry tests and update the reports as required. When a test is retried at least once, an R is printed to the live test output and the counter of retried tests is incremented by 1. After the test session has completed, an additional report is generated below the standard output which lists all of the tests which were retried, along with the exceptions that occurred during each failed attempt.

plugins: retry-1.1.0
collected 1 item

test_retry_passes_after_temporary_test_failure.py R.                     [100%]

======================= the following tests were retried =======================

	test_eventually_passes failed on attempt 1! Retrying!
	Traceback (most recent call last):
	  File "tests/test_example.py", line 4, in test_eventually_passes
	    assert len(a) > 1
	AssertionError: assert 1 > 1
	 +  where 1 = len([1])

=========================== end of test retry report ===========================


========================= 1 passed, 1 retried in 0.01s =========================

Tests which have been retried but eventually pass are counted as both retried and passed, and tests which have been retried but eventually fail are counted as both retried and failed. Skipped, xfailed, and xpassed tests are never retried.

Three pytest stash keys are available to import from the pytest_retry plugin: attempts_key, outcome_key, and duration_key. These keys are used by the plugin to store the number of attempts each item has undergone, whether the test passed or failed, and the total duration from setup to teardown, respectively. (If any stage of setup, call, or teardown fails, a test is considered failed overall). These stash keys can be used to retrieve these reports for use in your own hooks or plugins.

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-retry-1.2.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

pytest_retry-1.2.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest-retry-1.2.0.tar.gz.

File metadata

  • Download URL: pytest-retry-1.2.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for pytest-retry-1.2.0.tar.gz
Algorithm Hash digest
SHA256 aea554865cbe3b7356cc08e5a9ad11e097f7f38417db31ac34d9346036f44ca1
MD5 1c18f4524bd8ff75a4538283e5feb860
BLAKE2b-256 8b2e8c73d3249529e1c80a09869072af8bf6b07c7e040b287c528f26635a927c

See more details on using hashes here.

File details

Details for the file pytest_retry-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_retry-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for pytest_retry-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa6724a7a086ded2a1b0f03cede5f69e06f77558642d27246c7237293fd4ad28
MD5 b5fa3e1bebbb956c629cb64a7b46102a
BLAKE2b-256 ee1c818d18bec18cf2cb88289fc050d909383363e12f5745502bab1cc6bd44e7

See more details on using hashes here.

Supported by

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