一、Python中为False的数值断言均失败
在Python语言中表示False的数值即各个数据类型的空值,如整数的0,字符串的空字符串,None值,空列表、空字典、空集合、空元组,布尔类型的False等,这里需要注意的是一个空格的字符串的值不是False,编写测试脚本如下
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
def test_demo09():
assert " "
执行结果如下,可以看出有8个用例失败,有1个用例通过,通过的那个用例即对空格字符串的判断,即空格字符串的布尔值是True,而空字符串的布尔值则是False。
$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0
rootdir: G:\redrose2100_book\ebooks\Pytest企业级应用实战\src
collected 9 items
test_demo.py FFFFFFFF. [100%]
=============================================================================== FAILURES ===============================================================================
_____________________________________________________________________________ test_demo01 ______________________________________________________________________________
def test_demo01():
> assert 0
E assert 0
test_demo.py:2: AssertionError
_____________________________________________________________________________ test_demo02 ______________________________________________________________________________
def test_demo02():
> assert 0.0
E assert 0.0
test_demo.py:5: AssertionError
_____________________________________________________________________________ test_demo03 ______________________________________________________________________________
def test_demo03():
> assert ""
E AssertionError: assert ''
test_demo.py:8: AssertionError
_____________________________________________________________________________ test_demo04 ______________________________________________________________________________
def test_demo04():
> assert ()
E assert ()
test_demo.py:11: AssertionError
_____________________________________________________________________________ test_demo05 ______________________________________________________________________________
def test_demo05():
> assert {}
E assert {}
test_demo.py:14: AssertionError
_____________________________________________________________________________ test_demo06 ______________________________________________________________________________
def test_demo06():
> assert []
E assert []
test_demo.py:17: AssertionError
_____________________________________________________________________________ test_demo07 ______________________________________________________________________________
_____________________________________________________________________________ test_demo08 ______________________________________________________________________________
def test_demo08():
> assert False
E assert False
test_demo.py:23: 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, 1 passed in 0.16s ======================================================================
二、Python逻辑表达式为False的断言均失败
在Python中常用的逻辑判断表达式,比如==、!=、<、>、<=、>=、is、is not,in,not in,is None,is not None等,断言时均失败,编写测试用例代码如下
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
执行结果如下,即五个测试用例均失败。
pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0
rootdir: G:\redrose2100_book\ebooks\Pytest企业级应用实战\src
collected 5 items
test_demo.py FFFFF [100%]
=============================================================================== FAILURES ===============================================================================
_____________________________________________________________________________ test_demo01 ______________________________________________________________________________
def test_demo01():
> assert 1==2
E assert 1 == 2
test_demo.py:2: AssertionError
_____________________________________________________________________________ test_demo02 ______________________________________________________________________________
def test_demo02():
> assert 3<2
E assert 3 < 2
test_demo.py:5: AssertionError
_____________________________________________________________________________ test_demo03 ______________________________________________________________________________
def test_demo03():
test_demo.py:11: AssertionError
_____________________________________________________________________________ test_demo05 ______________________________________________________________________________
def test_demo05():
> assert 0 is None
E assert 0 is None
test_demo.py:14: 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.14s ===========================================================================
在Pytest中,断言assert语句的使用就是这么简单,主要看assert后的数值或者表达式的布尔值,如果为True,则用例通过,如果为False,则用例失败。
三、自定义断言报错信息
在自定义断言报错信息之前,首先观察一下默认的情况的报错信息,编写测试代码如下就
def test_demo():
a=5
assert a==10
执行结果如下,仔细观察也是能看出来错误原因的,只不过我们测试人员都是追求极致的简单,希望一样就能看出错误的原因。
$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0
rootdir: G:\redrose2100_book\ebooks\Pytest企业级应用实战\src
collected 1 item
test_demo.py F [100%]
=============================================================================== FAILURES ===============================================================================
______________________________________________________________________________ test_demo _______________________________________________________________________________
def test_demo():
a=5
> assert a==10
E assert 5 == 10
test_demo.py:3: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_demo - assert 5 == 10
========================================================================== 1 failed in 0.11s ===========================================================================
编写如下代码
def test_demo():
a=5
assert a==10 , "变量a期望是10,当前为%d" % a
执行结果如下,可以很快的从断言报错信息中看出断言失败的原因,即“变量a的值期望的是10,当前为5”,这样一来对自动化脚本调试和执行时定位问题等非常方便。
$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0
rootdir: G:\redrose2100_book\ebooks\Pytest企业级应用实战\src
collected 1 item
test_demo.py F [100%]
=============================================================================== FAILURES ===============================================================================
______________________________________________________________________________ test_demo _______________________________________________________________________________
def test_demo():
a=5
> assert a==10 , "变量a期望是10,当前为%d" % a
E AssertionError: 变量a期望是10,当前为5
E assert 5 == 10
test_demo.py:3: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_demo - AssertionError: 变量a期望是10,当前为5
========================================================================== 1 failed in 0.11s ===========================================================================