Repeat a test upon an AssertionError

Question:

There is the module pytest-repeat which can be used to repeat the execution of pytest N times (the whole test suite).

However, I want to, upon an AssertionError, re-run that specific test again, instead of running the whole suit. So, how can I capture the AssertionError and programmatically call the same test function?

Asked By: The Illusive Man

||

Answers:

The flaky package provides a @flaky decorator. From the documentation:

@flaky(max_runs=3, min_passes=2)
def test_something_that_usually_passes(self):
    """This test must pass twice, and it can be run up to three times."""
    value_to_double = 21
    result = get_result_from_flaky_doubler(value_to_double)
    self.assertEqual(result, value_to_double * 2, 'Result doubled incorrectly.')
Answered By: Florian Brucker
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.