I have a test which randomly fails and I want to let it retry a number of times before sending an error message.
I'm using python with Nose.
I wrote the following, but unfortunately, even with the try/except handling, Nose returns an error when the test fails on the first try.
def test_something(self):
maxAttempts = 3
func = self.run_something
attempt = 1
while True:
if attempt == maxAttempts:
yield func
break
else:
try:
yield func
break
except:
attempt += 1
def run_something(self):
#Do stuff
Thanks
You can use attributes on your functions with the flaky nose plugin that will automatically re-run tests and let you use advanced parameters (like if 2 in 3 test pass, then it's a pass)
GitHub flaky project
How to install Flaky plugin for Python:
Example nose test runner configuration:
Example Python code with function marked with Flaky attribute:
By using a generator, you're giving nose
maxAttempts
tests to run. if any of them fail, the suite fails. The try/catch doesn't particularly apply to the tests your yielding, since its nose that runs them. Rewrite your test like so: