在pytest中,断言就直接使用Python语言中的assert断言语句,这就做到了非常简单,不需要去记各种各样的断言语句,比如有的框架中封装了assertTrue,assertFalse之类的,其实有点画蛇添足,而pytest则坚持Python原生的简单性,直接使用assert语句,而断言语句的内容,则直接应用python中的布尔,只要assert后的语句结果为true,则断言成功,反之则断言失败
2.1.1 Python中被认为是False的值,断言均失败
如下,创建目录 pytest-demo/ch02/ex_001/,然后创建test_demo.py文件,内容如下,分别验证了整数、浮点数、字符串、元组、字典、列表等类型为0或者为空时,以及None和False,断言时均报错
测试脚本如下
# pytest-demo/ch02/ex_001/test_demo.py
def test_demo01():
assert 0
def test_demo02():
assert 0.0
def test_demo03():
assert ""
def test_demo04():
assert ()
def test_demo05():
assert {
}
def test_demo06():
assert []
def test_demo07():
assert None
def test_demo08():
assert False
在pycharm的terminal中切换到 pytest-demo/ch01/ex_001目录下,然后使用 pipenv run pytest 命令执行,结果如下,断言均报错,在最后显示有8个用例失败
G:\demo\pytest-demo\ch02\ex_001>pipenv run pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.10.4, pytest-7.1.2, pluggy-1.0.0
rootdir: G:\demo\pytest-demo\ch02\ex_001
collected 8 items
test_demo.py FFFFFFFF [100%]
=============================================================================== FAILURES ===============================================================================
_____________________________________________________________________________ test_demo01 ______________________________________________________________________________
def test_demo01():
> assert 0
E assert 0
test_demo.py:3: AssertionError
_____________________________________________________________________________ test_demo02 ______________________________________________________________________________
def test_demo02():
> assert 0.0
E assert 0.0
test_demo.py:6: AssertionError
_____________________________________________________________________________ test_demo03 ______________________________________________________________________________
def test_demo03():
> assert ""
E AssertionError: assert ''
test_demo.py:9: AssertionError
_____________________________________________________________________________ test_demo04 ______________________________________________________________________________
def test_demo04():
> assert ()
E assert ()
test_demo.py:12: AssertionError
_____________________________________________________________________________ test_demo05 ______________________________________________________________________________
def test_demo05():
> assert {
}
E assert {
}
test_demo.py:15: AssertionError
_____________________________________________________________________________ test_demo06 ______________________________________________________________________________
def test_demo06():
> assert []
E assert []
test_demo.py:18: AssertionError
_____________________________________________________________________________ test_demo07 ______________________________________________________________________________
def test_demo07():
> assert None
E assert None
test_demo.py:21: AssertionError
_____________________________________________________________________________ test_demo08 ______________________________________________________________________________
def test_demo08():
> assert False
E assert False
test_demo.py:24: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_demo01 - assert 0
FAILED test_demo.py::test_demo02 - assert 0.0
FAILED test_demo.py::test_demo03 - AssertionError: assert ''
FAILED test_demo.py::test_demo04 - assert ()
FAILED test_demo.py::test_demo05 - assert {
}
FAILED test_demo.py::test_demo06 - assert []
FAILED test_demo.py::test_demo07 - assert None
FAILED test_demo.py::test_demo08 - assert False
========================================================================== 8 failed in 0.17s ===========================================================================
G:\demo\pytest-demo\ch02\ex_001>
2.1.2 Python中结果为False的逻辑判断语句断言均失败
在python中常用的逻辑判断的语句,比如==,!=,<,>,<=,>=,is,is not,in,not in,is None,is not None等
如下,创建目录 pytest-demo/ch02/ex_002/,然后创建test_demo.py文件,编写测试代码如下,分别验证了逻辑为False的语句进行断言时的情况
def test_demo01():
assert 1==2
def test_demo02():
assert 3<2
def test_demo03():
assert 3 is str
def test_demo04():
assert 3 in [1,2,4]
def test_demo05():
assert 0 is None
在pycharm的terminal中,进入到pytest-demo/ch02/ex_002/目录下,执行pipenv run pytest 命令,结果如下,断言均失败,结尾显示有5个用例失败
G:\demo\pytest-demo\ch02\ex_002>pipenv run pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.10.4, pytest-7.1.2, pluggy-1.0.0
rootdir: G:\demo\pytest-demo\ch02\ex_002
collected 5 items
test_demo.py FFFFF [100%]
=============================================================================== FAILURES ===============================================================================
_____________________________________________________________________________ test_demo01 ______________________________________________________________________________
def test_demo01():
> assert 1==2
E assert 1 == 2
test_demo.py:3: AssertionError
_____________________________________________________________________________ test_demo02 ______________________________________________________________________________
def test_demo02():
> assert 3<2
E assert 3 < 2
test_demo.py:6: AssertionError
_____________________________________________________________________________ test_demo03 ______________________________________________________________________________
def test_demo03():
> assert 3 is str
E assert 3 is str
test_demo.py:9: AssertionError
_____________________________________________________________________________ test_demo04 ______________________________________________________________________________
def test_demo04():
> assert 3 in [1,2,4]
E assert 3 in [1, 2, 4]
test_demo.py:12: AssertionError
_____________________________________________________________________________ test_demo05 ______________________________________________________________________________
def test_demo05():
> assert 0 is None
E assert 0 is None
test_demo.py:15: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_demo01 - assert 1 == 2
FAILED test_demo.py::test_demo02 - assert 3 < 2
FAILED test_demo.py::test_demo03 - assert 3 is str
FAILED test_demo.py::test_demo04 - assert 3 in [1, 2, 4]
FAILED test_demo.py::test_demo05 - assert 0 is None
========================================================================== 5 failed in 0.16s ===========================================================================
G:\demo\pytest-demo\ch02\ex_002>
除此以外,当assert后面的变量或逻辑表达式为True时,则断言成功,即测试用例通过,这里就不再一一演示了
这就是pytest中的断言,至此可以发现,pytest中的断言的确非常简单,只需记住使用assert语句,assert后面的无论变量还是逻辑表达式,跟python语言中完全一致。