• Stars
    star
    351
  • Rank 116,417 (Top 3 %)
  • Language
    Python
  • License
    Other
  • Created over 11 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a pytest plugin that re-runs failed tests up to -n times to eliminate flakey failures

pytest-rerunfailures

pytest-rerunfailures is a plugin for pytest that re-runs tests to eliminate intermittent failures.

License

PyPI

GitHub Actions

Requirements

You will need the following prerequisites in order to use pytest-rerunfailures:

  • Python 3.8+ or PyPy3
  • pytest 7.2 or newer

This plugin can recover from a hard crash with the following optional prerequisites:

  • pytest-xdist 2.3.0 or newer

This package is currently tested against the last 5 minor pytest releases. In case you work with an older version of pytest you should consider updating or use one of the earlier versions of this package.

Installation

To install pytest-rerunfailures:

$ pip install pytest-rerunfailures

Recover from hard crashes

If one or more tests trigger a hard crash (for example: segfault), this plugin will ordinarily be unable to rerun the test. However, if a compatible version of pytest-xdist is installed, and the tests are run within pytest-xdist using the -n flag, this plugin will be able to rerun crashed tests, assuming the workers and controller are on the same LAN (this assumption is valid for almost all cases because most of the time the workers and controller are on the same computer). If this assumption is not the case, then this functionality may not operate.

Re-run all failures

To re-run all test failures, use the --reruns command line option with the maximum number of times you'd like the tests to run:

$ pytest --reruns 5

Failed fixture or setup_class will also be re-executed.

To add a delay time between re-runs use the --reruns-delay command line option with the amount of seconds that you would like wait before the next test re-run is launched:

$ pytest --reruns 5 --reruns-delay 1

Re-run all failures matching certain expressions

To re-run only those failures that match a certain list of expressions, use the --only-rerun flag and pass it a regular expression. For example, the following would only rerun those errors that match AssertionError:

$ pytest --reruns 5 --only-rerun AssertionError

Passing the flag multiple times accumulates the arguments, so the following would only rerun those errors that match AssertionError or ValueError:

$ pytest --reruns 5 --only-rerun AssertionError --only-rerun ValueError

Re-run all failures other than matching certain expressions

To re-run only those failures that do not match a certain list of expressions, use the --rerun-except flag and pass it a regular expression. For example, the following would only rerun errors other than that match AssertionError:

$ pytest --reruns 5 --rerun-except AssertionError

Passing the flag multiple times accumulates the arguments, so the following would only rerun those errors that does not match with AssertionError or OSError:

$ pytest --reruns 5 --rerun-except AssertionError --rerun-except OSError

Note

When the `AssertionError comes from the use of the assert keyword, use --rerun-except assert` instead:

$ pytest --reruns 5 --rerun-except assert

Re-run individual failures

To mark individual tests as flaky, and have them automatically re-run when they fail, add the flaky mark with the maximum number of times you'd like the test to run:

@pytest.mark.flaky(reruns=5)
def test_example():
    import random
    assert random.choice([True, False])

Note that when teardown fails, two reports are generated for the case, one for the test case and the other for the teardown error.

You can also specify the re-run delay time in the marker:

@pytest.mark.flaky(reruns=5, reruns_delay=2)
def test_example():
    import random
    assert random.choice([True, False])

You can also specify an optional condition in the re-run marker:

@pytest.mark.flaky(reruns=5, condition=sys.platform.startswith("win32"))
def test_example():
   import random
   assert random.choice([True, False])

Exception filtering can be accomplished by specifying regular expressions for only_rerun and rerun_except. They override the --only-rerun and --rerun-except command line arguments, respectively.

Arguments can be a single string:

@pytest.mark.flaky(rerun_except="AssertionError")
def test_example():
    raise AssertionError()

Or a list of strings:

@pytest.mark.flaky(only_rerun=["AssertionError", "ValueError"])
def test_example():
    raise AssertionError()

You can use @pytest.mark.flaky(condition) similarly as @pytest.mark.skipif(condition), see pytest-mark-skipif

@pytest.mark.flaky(reruns=2,condition="sys.platform.startswith('win32')")
def test_example():
    import random
    assert random.choice([True, False])
# totally same as the above
@pytest.mark.flaky(reruns=2,condition=sys.platform.startswith("win32"))
def test_example():
  import random
  assert random.choice([True, False])

Note that the test will re-run for any condition that is truthy.

Output

Here's an example of the output provided by the plugin when run with --reruns 2 and -r aR:

test_report.py RRF

================================== FAILURES ==================================
__________________________________ test_fail _________________________________

    def test_fail():
>       assert False
E       assert False

test_report.py:9: AssertionError
============================ rerun test summary info =========================
RERUN test_report.py::test_fail
RERUN test_report.py::test_fail
============================ short test summary info =========================
FAIL test_report.py::test_fail
======================= 1 failed, 2 rerun in 0.02 seconds ====================

Note that output will show all re-runs. Tests that fail on all the re-runs will be marked as failed.

Compatibility

  • This plugin may not be used with class, module, and package level fixtures.
  • This plugin is not compatible with pytest-xdist's --looponfail flag.
  • This plugin is not compatible with the core --pdb flag.
  • This plugin is not compatible with the plugin flaky, you can only have pytest-rerunfailures or flaky but not both.

Resources

Development

  • Test execution count can be retrieved from the execution_count attribute in test item's object. Example:

    @hookimpl(tryfirst=True)
    def pytest_runtest_makereport(item, call):
        print(item.execution_count)

More Repositories

1

pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
Python
11,150
star
2

pytest-testinfra

Testinfra test your infrastructures
Python
2,306
star
3

pytest-mock

Thin-wrapper around the mock package for easier use with pytest
Python
1,725
star
4

pytest-cov

Coverage plugin for pytest.
Python
1,647
star
5

pytest-xdist

pytest plugin for distributed testing and loop-on-failures testing modes.
Python
1,318
star
6

pytest-asyncio

Asyncio support for pytest
Python
1,293
star
7

pytest-django

A Django plugin for pytest.
Python
1,286
star
8

pytest-bdd

BDD library for the py.test runner
Python
1,248
star
9

pluggy

A minimalist production ready plugin system
Python
1,136
star
10

pytest-html

Plugin for generating HTML reports for pytest results
Python
649
star
11

pyfakefs

Provides a fake file system that mocks the Python file system modules.
Python
596
star
12

pytest-randomly

🎲 Pytest plugin to randomly order tests and control random.seed
Python
573
star
13

pytest-flask

A set of pytest fixtures to test Flask applications
Python
471
star
14

pytest-qt

pytest plugin for Qt (PyQt5/PyQt6 and PySide2/PySide6) application testing
Python
376
star
15

pytest-factoryboy

factory_boy integration the pytest runner
Python
348
star
16

pytest-selenium

Plugin for running Selenium with pytest
Python
325
star
17

cookiecutter-pytest-plugin

A Cookiecutter template for pytest plugins 💻
Python
281
star
18

pytest-splinter

pytest splinter and selenium integration for anyone interested in browser interaction in tests
Python
250
star
19

pytest-describe

Describe-style plugin for the pytest framework
Python
201
star
20

pytest-timeout

Python
185
star
21

pytest-subtests

unittest subTest() support and subtests fixture
Python
177
star
22

pytest-repeat

pytest plugin for repeating test execution
Python
158
star
23

pytest-order

pytest plugin that allows to customize the test execution order
Python
145
star
24

pytest-instafail

py.test plugin to show failures instantly
Python
130
star
25

unittest2pytest

helps rewriting Python `unittest` test-cases into `pytest` test-cases
Python
125
star
26

pytest-cpp

Use pytest's runner to discover and execute C++ tests
C++
111
star
27

pytest-github-actions-annotate-failures

Pytest plugin to annotate failed tests with a workflow command for GitHub Actions
Python
109
star
28

pytest-env

pytest plugin to set environment variables in pytest.ini or pyproject.toml file
Python
103
star
29

pytest-xprocess

pytest external process plugin
Python
94
star
30

pytest-reportlog

Replacement for the --resultlog option, focused in simplicity and extensibility
Python
75
star
31

pytest-variables

Plugin for providing variables to pytest tests/fixtures
Python
73
star
32

execnet

distributed Python deployment and communication
Python
72
star
33

pytest-play

pytest plugin that let you automate actions and assertions with test metrics reporting executing plain YAML files
Python
68
star
34

pytest-messenger

Pytest-messenger report plugin for all popular messengers like: Slack, DingTalk, Telegram
Python
67
star
35

py

Python development support library (note: maintenance only)
Python
66
star
36

pytest-random-order

pytest plugin to randomise the order of tests with some control over the randomness
Python
64
star
37

pytest-print

pytest-print adds the printer fixture you can use to print messages to the user (directly to the pytest runner, not stdout)
Python
63
star
38

pytest-mimesis

Mimesis integration with the pytest test runner. This plugin provider useful fixtures based on providers from Mimesis.
Python
62
star
39

pytest-services

Collection of fixtures and utility functions to run service processes for your tests
Python
58
star
40

pytest-forked

extracted --boxed from pytest-xdist to ensure backward compat
Python
58
star
41

pytest-runner

Python
57
star
42

pytest-metadata

Plugin for accessing test session metadata
Python
55
star
43

iniconfig

Python
49
star
44

apipkg

Python
48
star
45

pytest-twisted

test twisted code with pytest
Python
45
star
46

pytest-stress

A Pytest plugin that allows you to loop tests for a user defined amount of time.
Python
40
star
47

pytest-incremental

py-test plugin: an incremental test runner
Python
40
star
48

pytest-faker

faker integration the pytest test runner
Python
38
star
49

nose2pytest

Scripts to convert Python Nose tests to PyTest
Python
35
star
50

pytest-base-url

pytest plugin for URL based tests
Python
35
star
51

pytest-cloud

Distributed tests planner plugin for pytest testing framework.
Python
35
star
52

pytest-freezer

Pytest plugin providing a fixture interface for spulec/freezegun
Python
35
star
53

plugincompat

Test execution and compatibility checks for pytest plugins
CSS
34
star
54

pytest-fixture-tools

Pytest fixture tools
Python
32
star
55

pytest-faulthandler

py.test plugin that activates the fault handler module during testing
Python
27
star
56

pytest-echo

pytest plugin to dump environment variables, package version and generic attributes.
Python
21
star
57

pytest-localserver

py.test plugin to test server connections locally. This repository was migrated from Bitbucket.
Python
19
star
58

pytest-inline

pytest-inline is a pytest plugin for writing inline tests.
Python
14
star
59

pytest-nunit

An Nunit output plugin for Pytest
Python
10
star
60

design

Graphic design for Pytest project
9
star
61

pytest-plus

pytest-plus adds new features to pytest
Python
9
star
62

pytest-bpdb

pytest plugin for dropping to bpdb on test failures
Python
6
star
63

blog.pytest.org

Repository for the official pytest blog
Python
4
star
64

pytest-tcpclient

pytest mocking of TCP clients
Python
3
star
65

meta

Used for generic pytest organization issues, stuff that can impact multiple projects.
2
star
66

pytest-talks

public pytest talks and workshops - meant to help user groups spin up talks and workshops
HTML
1
star
67

regendoc

Python
1
star
68

sprint

pytest development sprint 2024
1
star