首页 > 其他 > 详细

pytest-断言

时间:2020-07-21 00:30:42      阅读:90      评论:0      收藏:0      [点我收藏+]

断言

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 ======================================

pytest-断言

原文:https://www.cnblogs.com/jingxindeyi/p/13348239.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!