zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛
    2577 医院设置
    2488 绿豆蛙的归宿
    3315 时空跳跃者的魔法
    1079 回家
    1365 浴火银河星际跳跃
    1074 食物链 2001年NOI全国竞赛
    2596 售货员的难题
    wetask.cn领度任务全新试用体验
    多线程--生产者消费者--简单例子
  • 原文地址:https://www.cnblogs.com/wang-mengmeng/p/11753982.html
Copyright © 2011-2022 走看看