本文介绍了PyUnit:在第一次测试失败后停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的测试框架中使用以下代码:

I'm using the following code in my testing framework:

testModules = ["test_foo", "test_bar"]

suite = unittest.TestLoader().loadTestsFromNames(testModules)
runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
results = runner.run(suite)
return results.wasSuccessful()

有没有办法让报告 (runner.run?) 在第一次失败后中止以防止过度冗长?

Is there a way to make the reporting (runner.run?) abort after the first failure to prevent excessive verbosity?

推荐答案

在提出这个问题九年后,这仍然是python 单元测试提前失败"的热门搜索结果之一,正如我在查看时发现的在其他搜索结果中,这些答案对于更新版本的 unittest 模块不再正确.

Nine years after the question was asked, this is still one of the top search results for "python unit test fail early" and, as I discovered when looking at the other search results, these answers are no longer correct for more recent versions of the unittest module.

unittest 模块的文档 https://docs.python.org/3/library/unittest.html#command-line-optionshttps://docs.python.org/2.7/library/unittest.html#command-line-options 显示有一个参数,failfast=True,可以添加到unittest.main 或等效的命令行选项 -f 或 --failfast,用于在第一个错误或失败时停止测试运​​行.此选项是在 2.7 版中添加的.使用该选项比其他答案中建议的以前必要的解决方法要容易得多.

The documentation for the unittest module https://docs.python.org/3/library/unittest.html#command-line-options and https://docs.python.org/2.7/library/unittest.html#command-line-options show that there is an argument, failfast=True, that can be added to unittest.main, or equivalently a command line option, -f, or --failfast, to stop the test run on the first error or failure. This option was added in version 2.7. Using that option is a lot easier than the previously-necessary workarounds suggested in the other answers.

也就是说,只需更改您的

That is, simply change your

unittest.main()

unittest.main(failfast=True)

这篇关于PyUnit:在第一次测试失败后停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:39