我更熟悉谷歌测试框架,并了解他们支持的主要行为对 ASSERT_*
对比 EXPECT_*
这是致命和非致命断言模式。
来自 documentation :
The assertions come in pairs that test the same thing but have different effects on the current function.
ASSERT_*
versions generate fatal failures when they fail, and abort the current function.EXPECT_*
versions generate nonfatal failures, which don't abort the current function. Usually EXPECT_* are preferred, as they allow more than one failures to be reported in a test. However, you should use ASSERT_* if it doesn't make sense to continue when the assertion in question fails.
问题 : pytest 是否也有我可以启用的非致命断言风格或模式?
允许最大程度地执行完整范围的测试以获得最丰富的故障历史记录而不是在第一次故障时中止并可能隐藏必须通过运行测试应用程序的多个实例分段发现的后续故障,这很好。
最佳答案
不,pytest
中没有类似的功能.最流行的方法是使用常规 assert
语句,如果表达式为假,则测试立即失败。
It's nice to allow a full range of tests to maximally execute to get the richest failure history rather than abort at the first failure and potentially hide subsequent failures that have to be discovered piecewise by running multiple instances of the test application.
关于这是否好,意见不一。至少在开源 Python 社区中,流行的方法是:每个潜在的“分段发现的后续故障”都将编写在自己的单独测试中。更多的测试,更小的测试,(理想情况下)只对一件事断言。
您可以通过附加到错误列表然后在测试结束时断言该列表为空来轻松地重新创建 EXPECT_* 事物,但在
pytest
中不直接提供支持。对于这样的功能。
关于python - pytest 是否有类似 google test 的非致命 EXPECT_* 行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50630845/