|
| 1 | +Mark Specific Tests as Flaky |
| 2 | +============================ |
| 3 | + |
| 4 | +The ``@pytest.mark.flaky`` decorator allows you to mark individual tests as flaky and configure them to |
| 5 | +automatically re-run a specified number of times upon failure. This is particularly useful for specific tests |
| 6 | +that are intermittently failing due to non-deterministic conditions (e.g., network latency, race conditions). |
| 7 | +That mark also allows to override global settings specified via :doc:`command-line options </cli>`. |
| 8 | + |
| 9 | +Basic Usage |
| 10 | +----------- |
| 11 | + |
| 12 | +To use the ``@pytest.mark.flaky`` decorator, include it in your test function and specify the number of retries using the ``reruns`` argument: |
| 13 | + |
| 14 | +.. code-block:: python |
| 15 | +
|
| 16 | + @pytest.mark.flaky(reruns=3) |
| 17 | + def test_example(): |
| 18 | + import random |
| 19 | + assert random.choice([True, False]) |
| 20 | +
|
| 21 | +In this example, ``test_example`` will automatically re-run up to 3 times if it fails. |
| 22 | + |
| 23 | +Additional Options |
| 24 | +------------------ |
| 25 | + |
| 26 | +The ``@pytest.mark.flaky`` decorator supports the following optional arguments: |
| 27 | + |
| 28 | +``reruns_delay`` |
| 29 | +^^^^^^^^^^^^^^^^ |
| 30 | + |
| 31 | +Specify a delay (in seconds) between re-runs. |
| 32 | + |
| 33 | +.. code-block:: python |
| 34 | +
|
| 35 | + @pytest.mark.flaky(reruns=5, reruns_delay=2) |
| 36 | + def test_example(): |
| 37 | + import random |
| 38 | + assert random.choice([True, False]) |
| 39 | +
|
| 40 | +This will retry the test 5 times with a 2-second pause between attempts. |
| 41 | + |
| 42 | +``condition`` |
| 43 | +^^^^^^^^^^^^^ |
| 44 | + |
| 45 | +Re-run the test only if a specified condition is met. |
| 46 | +The condition can be any expression that evaluates to ``True`` or ``False``. |
| 47 | + |
| 48 | +.. code-block:: python |
| 49 | +
|
| 50 | + import sys |
| 51 | +
|
| 52 | + @pytest.mark.flaky(reruns=3, condition=sys.platform.startswith("win32")) |
| 53 | + def test_example(): |
| 54 | + import random |
| 55 | + assert random.choice([True, False]) |
| 56 | +
|
| 57 | +In this example, the test will only be re-run if the operating system is Windows. |
| 58 | + |
| 59 | + |
| 60 | +``only_rerun`` |
| 61 | +^^^^^^^^^^^^^^ |
| 62 | + |
| 63 | +Re-run the test only for specific exception types or patterns. |
| 64 | +That overrides the :option:`--only-rerun` command-line option. |
| 65 | + |
| 66 | +.. code-block:: python |
| 67 | +
|
| 68 | + @pytest.mark.flaky(reruns=5, only_rerun=["AssertionError", "ValueError"]) |
| 69 | + def test_example(): |
| 70 | + raise AssertionError() |
| 71 | +
|
| 72 | +``rerun_except`` |
| 73 | +^^^^^^^^^^^^^^^^ |
| 74 | + |
| 75 | +Exclude specific exception types or patterns from triggering a re-run. |
| 76 | +That overrides the :option:`--rerun-except` command-line option. |
| 77 | + |
| 78 | +.. code-block:: python |
| 79 | +
|
| 80 | + @pytest.mark.flaky(reruns=5, rerun_except="AssertionError") |
| 81 | + def test_example(): |
| 82 | + raise ValueError() |
0 commit comments