---
title: Test Retry
description: 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](/test-engine/test-frameworks) 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](/test-engine/health) 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](/ci-insights/auto-retry).

Test retry runs on pushes to your default branch and in the runs the
[merge queue](/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](/test-engine/test-states), in the jobs that run the latest release
of the Mergify plugin.

## How it works

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.

> **Figure:** 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.
>
> Timing diagram, not to scale, of one job on a push to the default branch where two tests fail, with the default 5 retries per test. test_coupon fails on attempt 1, runs again right away and fails on attempt 2, then passes on attempt 3, so the job does not fail on it. Later in the job, test_po_box fails on attempt 1 and on each of its 5 retries, attempts 2 to 6, back to back. It has no retry left, so the job fails on it.

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](/test-engine/health) on your default
  branch: they fail every time, so a retry would only cost time;

- [quarantined](/test-engine/test-states) 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](/test-engine/prevention) checks for.

You can also keep a job or a test from retrying: see
[Turning it off](#turning-it-off).

## Settings

| 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](https://dashboard.mergify.com/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.

## Which jobs retry

A job retries tests when it runs the latest release of the Mergify plugin for
its [test framework](/test-engine/test-frameworks), 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.

## Turning it off

To turn test retry off for a repository, pick it at the top right of **Test
Engine > [Configuration](https://dashboard.mergify.com/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`](/cli/tests), 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)`.

## In your CI log

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

```text
🔁 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.
```

## Framework limits

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`](/test-engine/test-frameworks/playwright#wrap-your-playwright-config).
  No retry for a test of a serial group, nor under `failOnFlakyTests`.

- **Jest**: the configuration needs
  [`withMergify`](/test-engine/test-frameworks/jest#configuration) 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.
