• pytest8-skip与xfail


     skip(无条件跳过测试用例)与skipif(有条件跳过测试用例)

    # test_skip_function.py      函数级别
    import pytest
    import sys
    
    
    @pytest.mark.skip(reason='no way of currently testing this')
    def test_the_unknown():
        assert 1 == 1
    
    
    @pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")  # 有条件跳过测试用例
    def test_function():
        assert 1 == 1
    
    输出结果:D:myprojectpytest_demo>pytest -qs --tb=no test_skip_function.py
    ss
    2 skipped in 0.02 seconds
    # test_skip_class.py   类级别
    import pytest
    import sys
    
    
    @pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.6 or higher")
    class TestSkipClass:
        def test_class(self):
            "requires python3.6 or higher"
    
    输出结果:
    D:myprojectpytest_demo>pytest -qs --tb=no test_skip_class.py
    s
    1 skipped in 0.02 seconds
    # test_skip_module.py   模块级别,跳过整个模块的测试用例
    import pytest
    
    
    pytestmark = pytest.mark.skip(reason='nopass')  
    def test_the_unknown():
        assert 1 == 1
    
    
    def test_module():
        assert 1 == 1
    
    输出结果:
    D:myprojectpytest_demo>pytest -qs --tb=no test_skip_module.py
    ss
    2 skipped in 0.02 seconds

    xfail

    # test_xfail.py
    import pytest
    
    xfail = pytest.mark.xfail
    
    
    @xfail
    def test_hello():
        assert 0
    
    
    @xfail(run=False)
    def test_hello2():
        assert 0
    
    
    @xfail("hasattr(os, 'sep')")
    def test_hello3():
        assert 0
    
    
    @xfail(reason="bug 110")
    def test_hello4():
        assert 0
    
    
    @xfail('pytest.__version__[0] != "17"')
    def test_hello5():
        assert 0
    
    输出结果:
    D:myprojectpytest_demo>pytest -rx --tb=no test_xfail.py
    =================================================================================== test session starts ====================================================================================
    platform win32 -- Python 3.6.5, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
    rootdir: D:myprojectpytest_demo
    plugins: allure-pytest-2.8.5
    collected 7 items                                                                                                                                                                           
    
    test_xfail.py xxxxxxx                                                                                                                                                                 [100%]
    
    ================================================================================= short test summary info ==================================================================================
    XFAIL test_xfail.py::test_hello
    XFAIL test_xfail.py::test_hello2
      reason: [NOTRUN]
    XFAIL test_xfail.py::test_hello3
      condition: hasattr(os, 'sep')
    XFAIL test_xfail.py::test_hello4
      bug 110
    XFAIL test_xfail.py::test_hello5
      condition: pytest.__version__[0] != "17"
    XFAIL test_xfail.py::test_hello6
      reason: reason
    XFAIL test_xfail.py::test_hello7
    ================================================================================ 7 xfailed in 0.11 seconds =================================================================================

    skip/xfail with parametrize

    # test_skip_xfail_with_parametrize.py
    import pytest
    import sys
    
    
    @pytest.mark.parametrize(('n', 'expected'),
                             [(1, 2),
                              pytest.param(1, 0, marks=pytest.mark.xfail),
                              pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")),
                              (2, 3),
                              (3, 4),
                              (4, 5),
                              pytest.param(10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k"))])
    def test_increment(n, expected):
        assert n + 1 == expected
    
    
    # 输出结果
    D:myprojectpytest_demo>pytest -qs --tb=no test_skip_xfail_with_parametrize.py
    .xx...s
    4 passed, 1 skipped, 2 xfailed in 0.05 seconds
  • 相关阅读:
    收藏的一些前端学习的网址
    使用box-shadow 实现水波、音波的效果
    asyncjs,waterfall的使用
    兼容opacity的方法
    在php框架中写正规则表达式时的磕绊
    浏览器的渲染原理
    正规则表达式判断数字
    ie6,7,8不兼容rgba,写background时候不要写成rgba
    jquery-ias插件详解
    制作手机页面过程中遇到的一点问题
  • 原文地址:https://www.cnblogs.com/wang-mengmeng/p/11753982.html
Copyright © 2020-2023  润新知