Python 单元测试:Nose 失败时重试?
我有一个随机失败的测试,我想让它在发送错误消息之前重试多次。
我将 python 与 Nose 一起使用。
我写了以下内容,但不幸的是,即使使用 try/ except 处理,当第一次尝试测试失败时,Nose 也会返回错误。
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
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过 flaky noose 插件 在函数上使用属性,该插件会自动重新运行测试并让您使用高级参数(比如如果三分之二的测试通过,那么就通过了)
GitHub flaky 项目
如何安装 Flaky Python 插件:
鼻子测试运行器配置示例:
带有 Flaky 属性标记的函数的示例 Python 代码:
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:
通过使用生成器,您可以运行鼻子
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: