问题描述
我有一个随机失败的测试,我想让它重试几次,然后再发送错误消息.
I have a test which randomly fails and I want to let it retry a number of times before sending an error message.
我正在将Python与Nose配合使用.
I'm using python with Nose.
我写了以下内容,但不幸的是,即使使用try/except处理,当第一次尝试失败时,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
谢谢
推荐答案
使用生成器,您可以运行鼻子maxAttempts
测试.如果其中的任何失败,则套件失败. try/catch并不特别适用于您的收益率测试,因为它的鼻子在运行它们.像这样重写测试:
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:
def test_something(self):
maxAttempts = 3
func = self.run_something
attempt = 1
while True:
if attempt == maxAttempts:
func() # <<<--------
break
else:
try:
func() # <<<--------
break
except:
attempt += 1
def run_something(self):
#Do stuff
这篇关于Python单元测试:使用Nose重试失败吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!