View as Markdown

Test Retry

On a push to the default branch and in the merge queue, the Mergify plugin runs a failing test again, so a flaky failure no longer fails the build.


With test retry, the Mergify plugin runs a failing test again, in the same job, right away. If a retry passes, the test passes and the build stays green, where a flaky failure would otherwise turn your default branch red until someone reruns the job, or fail a merge queue check. Every attempt is still recorded, so the test’s health status still shows it as flaky.

Test retry runs one test again inside the job. To rerun a whole CI job that failed, see Auto-Retry.

Test retry runs on pushes to your default branch and in the runs the merge queue checks. Other pull request runs never retry, so a test a pull request makes flaky still fails there.

Test retry is on by default for every repository on a plan that includes quarantine, in the jobs that run the latest release of the Mergify plugin.

When a test fails, the plugin runs it again, until it passes or one of two limits stops it: the number of retries one test may get, and the job’s retry budget, the extra time the job may spend on retries.

Attempts test_coupon Attempt 1 fails. 1 Attempt 2 fails. 2 Attempt 3 passes. 3 test_po_box Attempt 1 fails. 1 Attempt 2 fails. 2 Attempt 3 fails. 3 Attempt 4 fails. 4 Attempt 5 fails. 5 Attempt 6 fails. 6 Job The job fails on test_po_box, and not on test_coupon. The job fails on test_po_box, and not on test_coupon. fails Attempts test_coupon Attempt 1 fails. 1 Attempt 2 fails. 2 Attempt 3 passes. 3 test_po_box Attempt 1 fails. 1 Attempt 2 fails. 2 Attempt 3 fails. 3 Attempt 4 fails. 4 Attempt 5 fails. 5 Attempt 6 fails. 6 Job The job fails on test_po_box, and not on test_coupon. The job fails on test_po_box, and not on test_coupon. fails
A failing test runs again right away, in the same job: test_coupon passes on attempt 3, so the job does not fail on it, while test_po_box fails all 6 attempts, its run and its 5 retries, and the job fails on it. Green passed, red failed. Not to scale.

The two limits work together:

  • Retries per Test caps how many times one test runs again. A test that keeps failing stops there, so it cannot use up the budget on its own.

  • Retry Budget is a share of how long the job’s tests usually take. Every retry in the job draws on it, in the order the failures happen. A test runs again only while what is left can pay for one more attempt of it; once it cannot, the failure stands. The plugin works out how long the tests usually take from the average duration of a test on your default branch times the number of tests it runs, and allows at least 4 seconds whatever that comes to.

For example, a job that runs 600 tests, each taking half a second on average, usually spends 300 seconds on them. At the default 5%, it may spend 15 more seconds on retries.

Some tests are never retried:

  • tests Mergify classifies as broken on your default branch: they fail every time, so a retry would only cost time;

  • quarantined tests: their failures already do not fail the job;

  • in the merge queue, a test new to the queue’s target branch: a pull request should not merge a test that is flaky from the start, which prevention checks for.

You can also keep a job or a test from retrying: see Turning it off.

Setting What it does Default
Retry Budget The extra time a job may spend on retries, as a percentage of its tests’ usual duration 5%
Retries per Test How many times one failing test may run again 5

A repository nobody configured uses these defaults. To change them, pick the repository at the top right of Test Engine > Configuration, change the budget or the retries per test in the Test Retry section, and click Save in the bar at the bottom of the page. The dashboard lets you set the budget from 1% to 50%, and the retries per test from 1 to 20.

A job retries tests when it runs the latest release of the Mergify plugin for its test framework, set up as for the rest of Test Engine: pytest, RSpec, Minitest, Vitest, Playwright, Jest or Mocha. A job whose plugin is not the latest release retries only on a repository where someone turned test retry on. Jobs that only upload JUnit reports never retry. The Test Retry section lists the jobs whose plugin is too old or missing, and offers to install or update it.

To turn test retry off for a repository, pick it at the top right of Test Engine > Configuration and turn off Retry Failing Tests in the Test Retry section. Turning it off keeps both settings, and turning it on again restores them. Changing test retry requires write access to the repository. View change history opens the activity log for every change to test retry.

A script or an agent does the same with mergify tests retry, which turns test retry on or off, changes a setting, and prints what is in effect.

To keep one job from retrying, set MERGIFY_TEST_RETRY_ENABLE to false in its environment. With pytest, you can also keep a single test from retrying with @pytest.mark.mergify(auto_retry=False).

The plugin says what test retry did at the end of the job log:

🔁 Test retry
Test retry: 2 tests failed, so Mergify retried them.
1 of them passed on a retry, so it does not fail this run.
Passed on a retry:
- tests/test_checkout.py::test_coupon (attempt 3)
1 of them failed every attempt, so this run fails on it.
Still failing:
- tests/test_checkout.py::test_po_box (6 attempts)
Test retry used 4.8 s of its 15.0 s budget.

Each framework retries a test its own way, and some cannot retry every test:

  • pytest: no retry under pytest-xdist’s --dist each.

  • Vitest: Vitest 4.1 or later, in Node workers. Each worker sizes the budget from the tests of the file it runs, and the workers spend it together.

  • Playwright: the configuration needs withMergify. No retry for a test of a serial group, nor under failOnFlakyTests.

  • Jest: the configuration needs withMergify and jest-circus. No retry for a test.concurrent test, nor for one Jest retries at the end of its block or with its whole describe block. The budget is sized from the tests of the file that failed, so it is often the 4-second minimum.

  • Mocha: in a parallel run, each worker sizes the budget from the tests of the file it runs, and the workers spend it together.

A framework’s own retries, such as pytest-rerunfailures, rspec-retry or Playwright’s retries, run first. Test retry only runs a test again once they have all failed.

Was this page helpful?