我正在寻找一种方法来运行 PyTest 单元测试中的所有断言,即使其中一些断言失败了。我知道必须有一个简单的方法来做到这一点。我检查了 CLI 选项并浏览了该站点以查找类似的问题/答案,但没有看到任何内容。抱歉,如果这已经得到回答。
例如,考虑以下代码片段,旁边是 PyTest 代码:
def parrot(i):
return i
def test_parrot():
assert parrot(0) == 0
assert parrot(1) == 1
assert parrot(2) == 1
assert parrot(2) == 2
默认情况下,执行在第一次失败时停止:
$ python -m pytest fail_me.py
=================== test session starts ===================
platform linux2 -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: /home/npsrt/Documents/repo/codewars, inifile:
collected 1 items
fail_me.py F
=================== FAILURES ===================
___________________ test_parrot ___________________
def test_parrot():
assert parrot(0) == 0
assert parrot(1) == 1
> assert parrot(2) == 1
E assert 2 == 1
E + where 2 = parrot(2)
fail_me.py:7: AssertionError
=================== 1 failed in 0.05 seconds ===================
我想要做的是让代码继续执行,即使在 PyTest 遇到第一次失败后也是如此。
原文由 Jeff Wright 发布,翻译遵循 CC BY-SA 4.0 许可协议
它运行了你所有的测试。您只编写了一个测试,并且该测试运行了!
如果您想要非致命的断言,如果断言失败(如 Google Test 的 EXPECT 宏),测试将继续进行,请尝试提供该功能 的 pytest-expect 。这是他们网站给出的示例:
您可以看到期望失败不会停止测试,并且会报告所有失败的期望: