前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest之pytest-assume同用例多断言,断言1失败会执行后续代码及断言2

Pytest之pytest-assume同用例多断言,断言1失败会执行后续代码及断言2

作者头像
王大力测试进阶之路
发布2020-05-29 09:27:35
3.1K0
发布2020-05-29 09:27:35
举报
文章被收录于专栏:橙子探索测试

一般我们做自动化测试时,一个用例会写多个断言,当第一个断言失败后,后面的代码就不会执行了,于是我们引进了pytest-assume插件可以解决断言失败后继续断言的问题。

一、安装依赖包

pip install pytest-assume

二、使用assert进行断言

我们发现test_01用例中,第一个断言assert 0 == 1失败时,后面的代码不会再执行, print('执行test_01断言2')和assert 1 == 2未执行。

代码语言:javascript
复制
#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytest
 
 
class Test(object):
 
    def test_01(self):
        """用例1"""
        print('执行test_01断言1')
        assert 0 == 1
        print('执行test_01断言2')
        assert 1 == 2
 
    def test_02(self):
        """用例2"""
        print('执行test_02断言1')
        assert 3 == 3
        print('执行test_02断言2')
        assert 4 == 4
 
 
 
 
if __name__ == '__main__':
    pytest.main(['-s', 'test_C_01.py'])
 
 
C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/代码/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\代码\Test\test
plugins: assume-2.2.1
收集的测试用例:[<Function test_01>, <Function test_02>]
排序后收集的测试用例:[<Function test_01>, <Function test_02>]
collected 2 items
 
test_C_01.py 执行test_01断言1
F执行test_02断言1
执行test_02断言2
.
 
================================== FAILURES ===================================
________________________________ Test.test_01 _________________________________
 
self = <test.test_C_01.Test object at 0x0000018C74AAEF48>
 
    def test_01(self):
        """用例1"""
        print('执行test_01断言1')
>       assert 0 == 1
E       assert 0 == 1
 
test_C_01.py:11: AssertionError
=========================== short test summary info ===========================
FAILED test_C_01.py::Test::test_01 - assert 0 == 1
========================= 1 failed, 1 passed in 0.06s =========================
 
Process finished with exit code 0

二、使用pytest.assume进行断言,用法pytest.assume(预期结果 == 实际结果)

我们发现test_01用例中,第一个断言pytest.assume(0 == 1)失败时,print('执行test_01断言2')和pytest.assume(1 == 2)仍执行了,说明同一个用例中使用pytest.assume进行断言,如果第一个断言失败了,后面的代码仍会继续执行。

代码语言:javascript
复制
#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytest
 
 
class Test(object):
 
    def test_01(self):
        """用例1"""
        print('执行test_01断言1')
        pytest.assume(0 == 1)
        print('执行test_01断言2')
        pytest.assume(1 == 2)
 
    def test_02(self):
        """用例2"""
        print('执行test_02断言1')
        pytest.assume(3 == 3)
        print('执行test_02断言2')
        pytest.assume(4 == 4)
 
 
 
 
if __name__ == '__main__':
    pytest.main(['-s', 'test_C_01.py'])
 
 
C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/代码/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\代码\Test\test
plugins: assume-2.2.1
收集的测试用例:[<Function test_01>, <Function test_02>]
排序后收集的测试用例:[<Function test_01>, <Function test_02>]
collected 2 items
 
test_C_01.py 执行test_01断言1
执行test_01断言2
F执行test_02断言1
执行test_02断言2
.
 
================================== FAILURES ===================================
________________________________ Test.test_01 _________________________________
 
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None
 
    def reraise(tp, value, tb=None):
        try:
            if value is None:
                value = tp()
            if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption: 
E               2 Failed Assumptions:
E               
E               test_C_01.py:12: AssumptionFailure
E               >>  pytest.assume(0 == 1)
E               AssertionError: assert False
E               
E               test_C_01.py:15: AssumptionFailure
E               >>  pytest.assume(1 == 2)
E               AssertionError: assert False
 
..\..\..\..\AppData\Roaming\Python\Python37\site-packages\six.py:702: FailedAssumption
=========================== short test summary info ===========================
FAILED test_C_01.py::Test::test_01 - pytest_assume.plugin.FailedAssumption: 
========================= 1 failed, 1 passed in 0.20s =========================
 
Process finished with exit code 0
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 橙子探索测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档