pytest--assert断言

前言

断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了。什么是断言呢?

简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试failed

assert

pytest允许您使用标准Python断言来验证Python测试中的期望和值。例如,你可以写下

#test.sa.py
def f():
    return 3
def test_a():
    assert f()==4

断言f()函数的返回值,接下来会看到断言失败,E assert 3==4,因为返回的值是3,判断等于4.所以失败了。

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 1 item

test_f2.py F
test_f2.py:5 (test_a)
3 != 4

Expected :4
Actual   :3
<Click to see difference>

def test_a():
>       assert f()==4
E       assert 3 == 4
E        +  where 3 = f()

test_f2.py:7: AssertionError
                                                             [100%]

================================== FAILURES ===================================
___________________________________ test_a ____________________________________

    def test_a():
>       assert f()==4
E       assert 3 == 4
E        +  where 3 = f()

test_f2.py:7: AssertionError
=========================== short test summary info ===========================
FAILED test_f2.py::test_a - assert 3 == 4
============================== 1 failed in 0.26s ==============================

异常信息

接下来再看一个案例,如果想在异常的时候,输出一些提示信息,这样报错后,就方便查看是什么原因了。

#test.sa.py
def f():
    return 3
def test_a():
    a=f()
    assert a%2==0,"判断a为偶数?a的值:{}".format(a)

运行结果

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 1 item

test_f2.py F
AssertionError: 判断a为偶数?a的值:3
(3 % 2) != 0

Expected :0
Actual   :(3 % 2)
<Click to see difference>

def test_a():
        a=f()
>       assert a%2==0,"判断a为偶数?a的值:{}".format(a)
E       AssertionError: 判断a为偶数?a的值:3
E       assert (3 % 2) == 0

test_f2.py:8: AssertionError
                                                             [100%]

================================== FAILURES ===================================
___________________________________ test_a ____________________________________

    def test_a():
        a=f()
>       assert a%2==0,"判断a为偶数?a的值:{}".format(a)
E       AssertionError: 判断a为偶数?a的值:3
E       assert (3 % 2) == 0

test_f2.py:8: AssertionError
=========================== short test summary info ===========================
FAILED test_f2.py::test_a - AssertionError: 判断a为偶数?a的值:3
============================== 1 failed in 0.28s ==============================

这样当断言失败的时候,会给出自己写的失败原因了E AssertionError: 判断a为偶数?a的值:3

异常断言

为了写关于引发异常的断言,可以使用pytest.raise作为上下文管理器,如下

#test.sa.py
import pytest
def test_z():
    with pytest.raises(ZeroDivisionError):
        1/0
        #1/2#报错 

运行结果

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 1 item

test_f2.py .                                                             [100%]

============================== 1 passed in 0.11s ==============================

如果我们要断言它抛出的异常是不是预期的,比如执行:1/0,预期结果是抛出异常:ZeroDivisionError:division by zero,那我们要断言这个异常,通常是断言异常的type和value值了。

这里1/0的异常类型是ZeroDivisionError,异常的value值是division by zero,于是用例可以这样设计。

import pytest
import pytest
def test_z():
    with pytest.raises(ZeroDivisionError) as excinfo:
        1/0
    # 断言异常类型type
    assert excinfo.type==ZeroDivisionError
    #断言异常value值
    print(f'excinfo.type:{excinfo.type}')
    print(f'excinfo.value{excinfo.value}')
    assert "division by zero" in str(excinfo.value)
    #assert "dd" in str(excinfo.value)#断言失败
if __name__=="__main__":
    pytest.main(["test_f2.py"])
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 1 item

test_f2.py .excinfo.type:<class 'ZeroDivisionError'>
excinfo.valuedivision by zero
                                                             [100%]

============================== 1 passed in 0.12s ==============================

excinfo是一个异常信息实例,它是围绕实际引发的异常的包装器。主要属性是.type 、.value和.traceback

注意:断言type的时候,异常类型是不需要加引号的,断言value值的时候需要转str

常用断言

pytest里面断言实际上就是python里面的assert断言方法,常用的有以下几种;

assert xx判断xx为真

assert not xx判断xx不为真

assert a in b 判断b包含a

assert a==b 判断a等于b

assert a!=b 判断a不等于b

#test.sa.py
import pytest
def is_true(a):
    if a>0:
        return True
    else:
        return False
def test_10():
    a=5
    b=-1
    assert is_true(a)#真
    assert is_true(b) #假
def test_20():
    a="he"
    b="h he"
    assert a in b#真
    #assertIn(a,b)#报错#unittest中语法
def test_30():
    a="py"
    b="py"
    assert a==b#真
    #assertEqual(a,b)#报错,assertEqual未定义
def test_40():
    a=5
    b=6
    assert a!=b#真
 
if __name__=="__main__":
    #pytest.main(["-s","test_sa.py"])
    pytest.main(["-s","test_case1.py"])#和上面执行结果一样
上一篇:Pytest


下一篇:Pytest之断言