zoukankan      html  css  js  c++  java
  • pytest扫盲21--pytest-assume多重效验插件

    1、目的:

      解决多个assert断言中,其中一个断言失败,后续断言不再继续执行的问题。

    2、使用插件:

      pytest-assume

    3、安装: 

    pip install pytest-assume -i https://pypi.tuna.tsinghua.edu.cn/simple
    

      

    4、assert 多重断言:

    # File  : test_demo_18_assume.py
    # IDE   : PyCharm
    
    a = 6
    b = 3
    def test_calc_1():
        assert a / b == 2
        assert a + b == 8
        assert a * b == 18
        assert a - b == 2
        print('测试结束!')

      执行结果,遇到断言失败即终止测试,最后的 print 没有打印:

    E:personalpython38python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
    test_demo_18_assume.py::test_calc_1
    当前用例运行测试环境:http://xx.xx.xx.xx:xxxx
    F
    ================================== FAILURES ===================================
    _________________________________ test_calc_1 _________________________________
    
        def test_calc_1():
            assert a / b == 2
    >       assert a + b == 8
    E       assert (6 + 3) == 8
    
    test_demo_18_assume.py:12: AssertionError
    =========================== short test summary info ===========================
    FAILED test_demo_18_assume.py::test_calc_1 - assert (6 + 3) == 8
    1 failed in 0.13s
    
    Process finished with exit code 0

    5、assume 断言

    import pytest
    
    def test_calc_2():
        pytest.assume(a / b == 2)
        pytest.assume(a + b == 8)
        pytest.assume(a * b == 18)
        pytest.assume(a - b == 2)
        print('测试结束!')

      执行结果,后面断言即使失败,仍然继续执行后面的代码。

    E:personalpython38python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
    test_demo_18_assume.py::test_calc_2
    当前用例运行测试环境:http://xx.xx.xx.xx:xxxx
    测试结束!
    F
    ================================== FAILURES ===================================
    _________________________________ test_calc_2 _________________________________
    
    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_demo_18_assume.py:21: AssumptionFailure
    E               >>    pytest.assume(a + b == 8)
    E               AssertionError: assert False
    E               
    E               test_demo_18_assume.py:23: AssumptionFailure
    E               >>    pytest.assume(a - b == 2)
    E               AssertionError: assert False
    
    ....python38libsite-packagessix.py:702: FailedAssumption
    =========================== short test summary info ===========================
    FAILED test_demo_18_assume.py::test_calc_2 - pytest_assume.plugin.FailedAssum...
    1 failed in 0.19s
    
    Process finished with exit code 0
  • 相关阅读:
    Java基础知识点总结(四)
    Java基础知识点总结(三)
    用easyui实现查询条件的后端传递并自动刷新表格的两种方法
    在JDBC中实现SQL语句的模糊查询
    向DataGrid数据表格增加查询搜索框
    Java基础知识点总结(二)
    easyui中formatter的用法
    腾讯云+阿里云 搭建hadoop + hbase
    -- 记录 -- 问题记录
    -- 1 -- springboot
  • 原文地址:https://www.cnblogs.com/xiaohuboke/p/13572905.html
Copyright © 2011-2022 走看看