目录:
断言的编写和报告
使用assert语句进行断言
pytest允许你使用标准的Pythonassert断言语句来验证测试中的期望结果和实际结果。 例如,你可以编写以下内容:
# test_assert1.py文件内容
def f():
return 3
def test_function():
assert f() == 4
来断言你的函数返回一个特定的值。 如果此断言失败,你将看到函数调用的返回值:
$ pytest test_assert1.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 1 item
test_assert1.py F [100%]
================================= FAILURES =================================
______________________________ test_function _______________________________
def test_function():
> assert f() == 4
E assert 3 == 4
E + where 3 = f()
test_assert1.py:5: AssertionError
========================= 1 failed in 0.12 seconds =========================
pytest