Skip to main content
 首页 » 编程设计

python之如果发生错误,nosetests 重新启动测试

2025年05月04日8langtianya

我正在使用 nose 的 TestSuite 在我的 python 项目中进行单元测试。

问题是我的项目与第三方沙箱链接并通过 api 进行通信,但有时,错误来自他们这边。因此,我试图找到一种方法,在测试因特定错误而失败时重新启动测试。

是否有人已经在 TestSuite 中完成了这项工作?或者知道怎么做?

谢谢

请您参考如下方法:

1) 我经常在测试中重试远程代码,而不是重新运行整个测试。

2) 但是,如果您真的想重复所有失败的测试,并且没有人提出更好的解决方案,您可以编写一个自定义测试运行器,使用类似 api 的东西

#in unittest.TestCase 
retryManager.logFailure(test=self, error) 
 
#where you run tests 
for test in retryManager.failedTests 
    suite = unittest.TestLoader().loadTestsFromTestCase(testclass) 
    testResult = unittest.TextTestRunner(verbosity=2).run(suite) 

如果您不想捕获异常,您还可以使用 testResult.wasSuccessful() 查看测试是否成功

RetryManager 只是一种失败的存储库/日志。

3) 使用 pytest,它内置了这个功能:http://pytest.org/latest/xdist.html#looponfailing