pytest-断言

断言

pytest允许在assert关键字后面添加任何表达式(assert )。如果表达式的值通过bool转换后等于False,则意味着用例执行失败。

断言声明

#!/usr/bin/python3
#-*- conding:utf-8 -*-
 
def test_one():
    assert 1 == 2
pytest
==================================== test session starts =====================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 1 item                                                                             

test_001.py F                                                                          [100%]

========================================== FAILURES ==========================================
__________________________________________ test_one __________________________________________

    def test_one():
>       assert 1 == 2
E       assert 1 == 2

test_001.py:5: AssertionError
================================== short test summary info ===================================
FAILED test_001.py::test_one - assert 1 == 2
===================================== 1 failed in 0.17s ======================================

断言后添加说明

#!/usr/bin/python3
#-*- conding:utf-8 -*-
 
def test_one():
    a = 1
    b = 2
    assert a == b, "判断%s = %s"%(a,b)
==================================== test session starts =====================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 1 item                                                                             

test_001.py F                                                                          [100%]

========================================== FAILURES ==========================================
__________________________________________ test_one __________________________________________

    def test_one():
        a = 1
        b = 2
>       assert a == b, "判断%s = %s"%(a,b)
E       AssertionError: 判断1 = 2
E       assert 1 == 2

test_001.py:7: AssertionError
================================== short test summary info ===================================
FAILED test_001.py::test_one - AssertionError: 判断1 = 2
===================================== 1 failed in 0.21s ======================================

异常的断言

断言异常类型

#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest

def test_one():
    a = "test"
    with pytest.raises(ValueError):
        int(a)
==================================== test session starts =====================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 1 item                                                                             

test_001.py .                                                                          [100%]

===================================== 1 passed in 0.01s ======================================

断言异常的值

#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest

def test_one():
    a = "test"
    with pytest.raises(ValueError) as f:
        int(a)

    assert f.type == ValueError
    assert "invalid" in str(f.value)

==================================== test session starts =====================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 1 item

test_001.py . [100%]

===================================== 1 passed in 0.01s ======================================

posted @ 2020-07-20 22:37  静心&得意  阅读(257)  评论(0编辑  收藏  举报