前言
断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了。什么是断言呢?
简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试 failed
assert
pytest允许您使用标准Python断言来验证Python测试中的期望和值。例如,你可以写下
# content of test_assert1.py
def f():
return 3
def test_function():
assert f() == 4
断言f()函数的返回值,接下来会看到断言失败,因为返回的值是3,判断等于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%]
=======