python - pytest 是否有类似 google test 的非致命 EXPECT_* 行为?

标签 python pytest googletest

我更熟悉谷歌测试框架,并了解他们支持的主要行为对 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/

相关文章:

python - 列表对象没有属性分割

python - Docker runner pytest 不收集测试用例

python-3.x - 在 pytest 中,如何根据 bash shell 表达式设置环境变量?

python - PyTest:跳过整个模块/文件(python 2 和 3)

c++ - 为模板类创建 gtest 值参数化测试

python - 是否可以在类中添加静态类型?

python - 在 Python 中获取计算机的 UTC 偏移量

tdd - 如何在Google测试中为多个测试初始化​​常量字符串?

python - 在 SQL 语句中解压元组

cmake - 使用 CMake,如何在 gtest_discover_tests --gtest_list_tests 调用上设置环境属性?