2.2 自定义断言报错内容

2.2.1 默认情况下的断言报错信息

编写判断两个数是否先等的一个测试用例,脚本在仓库见 pytest-demo/ch02/ex_003/test_demo.py,代码内容如下

def test_demo():
    a=5
    assert a==10

执行结果如下:

G:\demo\pytest-demo\ch02\ex_003>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_003
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:4: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_demo - assert 5 == 10
========================================================================== 1 failed in 0.11s ===========================================================================

G:\demo\pytest-demo\ch02\ex_003>

从报错信息可以看出,这里是在断言5==10,具体啥意思的,还得继续向上看,上一句 assert a == 10,到这里可能还是迷糊,这里的a又是啥呢,这就是默认的断言存在的问题,也就是很难从断言报错的位置很快地找到原因,其实断言是可以自己定义断言报错时显示的信息的

2.2.2 自定义断言报错信息

如下,代码存放于 pytest-demo/ch02/ex_004/test.demo.py,在写断言的时候加了一句描述

def test_demo():
    a=5
    assert a==10 , "变量a期望是10,当前为%d" % a

此时的执行结果如下,通过下面的报错信息,一眼就看出来了原来这里变量a期望是10,当前为5,当然这里只是一个演示,在实际业务测试中,完全可以结合业务做更详细的描述,这样脚本报错后一眼就能看出来报错的原因了。这就是自定义断言的内容,不要小看了这个功能,这个功能用好了在后面脚本连跑维护的时候对脚本报错定位提bug等作用非常大,从这些细节处也能看出来是否一个专业的自动化测试人员

G:\demo\pytest-demo\ch02\ex_004>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_004
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:4: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_demo - AssertionError: 变量a期望是10,当前为5
========================================================================== 1 failed in 0.11s ===========================================================================

G:\demo\pytest-demo\ch02\ex_004>

猜你喜欢

转载自blog.csdn.net/redrose2100/article/details/125176163
2.2