Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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(configrun.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_cleanupandslash.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_startandtest_endfire for every attempt, so plugins that prepare and reset per test (e.g. a test stand) do so again, and a newtest_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.-xacts 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,--retriesconfig, 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 withbefore/after, duration across attempts, argument validation, and-xwith a flaky first test. I also checkedslash run --retries 1from the command line against a test that fails once.pytest testspasses locally (1937 passed, 6 skipped; Python 3.12, macOS) andruff check .is clean. Docs: a "Retrying Failed Tests" section inslash_run.rst,slash.retryin the API reference, and a changelog entry.Retries happen inside each worker's own runner, so
--parallelneeds nothing extra:slash run --parallel 2 --retries 1on 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.