我正在寻找一种在PyTest中运行单元测试中所有断言的方法,即使其中一些断言失败。我知道一定有一个简单的方法可以做到这一点。我检查了CLi选项,并在该网站上查找类似的问题/答案,但没有看到任何内容。抱歉,如果已经回答了这个问题
例如,考虑下面的代码片段,连同它的PyTestCube:
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遇到第一次失败后继续执行
正如其他人已经提到的,理想情况下,您应该编写多个测试,并且每个测试中只有一个断言(这不是一个硬限制,而是一个很好的指南)
^{} 装饰器使这变得很容易:
使用
-v
运行时:它运行了你所有的测试。您只编写了一个测试,并且该测试已运行
如果您想要非致命断言,如果断言失败,测试将继续进行(如Google test的EXPECT宏),请尝试提供该功能的pytest-expect。以下是他们网站给出的示例:
您可以看到,预期失败不会停止测试,所有失败的预期都会得到报告:
您应该能够使用
--maxfail
参数来控制这一点。我相信默认情况下不会因为失败而停止,所以我会检查您可能拥有的任何py.test配置文件,查找覆盖它的位置相关问题 更多 >
编程相关推荐