def test_something(self):
errors = []
# replace assertions by conditions
if not condition_1:
errors.append("an error message")
if not condition_2:
errors.append("an other error message")
# assert no error message has been registered, else print messages
assert not errors, "errors occured:\n{}".format("\n".join(errors))
import pytest_check as check
def test_example():
a = 1
b = 2
c = [2, 4, 6]
check.greater(a, b)
check.less_equal(b, a)
check.is_in(a, c, "Is 1 in the list")
check.is_not_in(b, c, "make sure 2 isn't in list")
import pytest
def test_sample(texts):
flag = True
for text in texts:
if text != "anything":
flag = False
if flag==False:
pytest.fail("text did not match", pytrace=True)
#!/usr/bin/env python3
"""
Multiple independent asserts using class-scope fixture
"""
import pytest
@pytest.fixture(scope="class")
def data():
"""
Create data for test.
Using scope=class, this fixture is created once per class. That
means each test should exercise care not to alter the fixture data,
or subsequent tests might fail.
"""
fixture_data = dict(a=1, b=2, c=3)
print(f"(data fixture created at {id(fixture_data)}) ", end="")
return fixture_data
class TestItWithFailures:
def test_a_value(self, data):
assert data["a"] == 1
# Modify the data will cause test failure in subsequent tests
data["b"] = 200
data["c"] = 300
def test_b_value(self, data):
# Failed because of previous modification
assert data["b"] == 2
def test_c_value(self, data):
# Failed because of previous modification
assert data["c"] == 3
class TestWithSuccess:
def test_a_value(self, data):
assert data["a"] == 1
def test_b_value(self, data):
assert data["b"] == 2
def test_c_value(self, data):
assert data["c"] == 3
输出
test_it.py::TestItWithFailures::test_a_value (data fixture created at 4366616320) PASSED
test_it.py::TestItWithFailures::test_b_value FAILED
test_it.py::TestItWithFailures::test_c_value FAILED
test_it.py::TestWithSuccess::test_a_value (data fixture created at 4372781312) PASSED
test_it.py::TestWithSuccess::test_b_value PASSED
test_it.py::TestWithSuccess::test_c_value PASSED
6条答案
按热度按时间2o7dmzc51#
正如Jon Clements所评论的,您可以填充一个错误消息列表,然后Assert该列表为空,当Assert为假时显示每条消息。
具体地说,可能是这样的:
原来的Assert被
if
语句所取代,if
语句在条件不满足时将消息附加到errors
列表中,然后Asserterrors
列表为空(空列表为False),并使Assert消息包含errors
列表中的每一条消息。你也可以像nose文档中描述的那样做一个测试生成器,我没有找到任何描述它的pytest文档,但是我知道pytest处理这个的方式和nose完全一样。
gzjq41n42#
pytest-assume是 “一个pytest插件,允许每个测试多次失败”。下面是一个如何使用它的示例(摘自
README
):即使有些Assert失败了,它们也会得到评估和报告:
oxcyiej73#
2017年关于pytest的Pragmatic一书的作者Brian Okken提供了另一个库。https://pythontesting.net/books/pytest/https://github.com/okken/pytest-check
drnojrws4#
这里有一个叫做Delayed assert的替代方法,它与@Tryph提供的方法非常相似,并且提供了更好的堆栈跟踪。
PyPI上的delayed-assert包实现了这种方法。另请参见GitHub上的pr4bh4sh/python-delayed-assert仓库,或使用以下命令从PyPI安装:
您可以将任何Assert库与python-delayed-assert结合使用。请将其视为更像堆栈跟踪管理器库而不是Assert。检查this以了解示例用法
这是错误堆栈跟踪的外观,
q43xntqr5#
下面是一个相当简单的方法:
rn0zuynd6#
有几个选项:
1.使用pytest-check
1.使用pytest-assume
1.使用pytest夹具
其他人已经为前两个提供了示例,我将讨论最后一个选项。
pytest文档中提供的示例稍微详细一些,我将提供一个更简单的示例。
输出
注解
TestItWithFailures
类中的后续测试。