Skip to content

Add test retries (--retries and @slash.retry) - #1093

Closed
Mohith26 wants to merge 1 commit into
getslash:developfrom
Mohith26:feat/retry-failed-tests
Closed

Mohith26 wants to merge 1 commit into
getslash:developfrom
Mohith26:feat/retry-failed-tests

Conversation

@Mohith26

@Mohith26 Mohith26 commented Nov 9, 2026

Copy link
Copy Markdown

Closes #1079.

Slash has no way to retry a flaky test. Wrapping the test body in a retry loop (e.g. with tenacity) doesn't work well, because the test's fixtures and cleanups from the failed attempt are still in place, which is what #1079 ran into.

This adds retries at the runner level:

  • --retries NUM (config run.retries, default 0) retries every failing test up to NUM more times.
  • @slash.retry(NUM) sets the count for one test or test method and takes precedence over --retries (so @slash.retry(0) opts a test out).

Behaviour

Each retry is a fresh attempt. After a failed attempt the test scope is popped as usual, so test-scoped fixtures and cleanups (including this.add_cleanup and slash.add_cleanup) are torn down, then a new test scope is pushed and the fixtures are created again. Module and session fixtures are kept. test_start and test_end fire for every attempt, so plugins that prepare and reset per test (e.g. a test stand) do so again, and a new test_retry(attempt, max_attempts) hook fires before each retry. The summary hooks (test_success/test_failure/test_error) fire once, for the final outcome.

The test is reported once, with one result: it succeeds if a retry succeeds, otherwise it carries the errors and failures of its last attempt. The earlier attempts' errors and failures are kept on the result (result.get_retried_attempts(), result.get_num_retries()), and the duration covers all attempts. Skips, interruptions and fatal errors are never retried. -x acts on the final outcome only.

Testing

New tests/test_retry.py (17 tests): flaky test passing on retry, a test failing every attempt (one error reported, 3 runs), no retry for passing tests or by default, --retries config, marker overriding config, exact setup/cleanup ordering for test- and module-scoped fixtures across attempts, hook order across attempts, skips not retried, an error raised by a test cleanup triggering a retry, class-based tests with before/after, duration across attempts, argument validation, and -x with a flaky first test. I also checked slash run --retries 1 from the command line against a test that fails once.

pytest tests passes locally (1937 passed, 6 skipped; Python 3.12, macOS) and ruff check . is clean. Docs: a "Retrying Failed Tests" section in slash_run.rst, slash.retry in the API reference, and a changelog entry.

Retries happen inside each worker's own runner, so --parallel needs nothing extra: slash run --parallel 2 --retries 1 on a file with two tests that fail once each reports 3 successful, with each flaky test run twice. There is no automated test for the parallel case.

Failing tests can be retried with --retries NUM (run.retries) or per
test with @slash.retry(NUM). Each retry pops and re-pushes the test
scope, so test-scoped fixtures and cleanups are recreated, and
test_start/test_end fire per attempt; a new test_retry hook fires
before each retry. The result keeps earlier attempts' errors and
failures. Skips, interruptions and fatal errors are not retried.

Closes #1079
@Mohith26 Mohith26 closed this by deleting the head repository Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Retries for flaky tests

1 participant