• pytest skip的使用


    skip跳过用例(无条件跳过,不运行用例)

    使用方法:

    1.使用跳过装饰器

    class TestClass():
        @pytest.mark.skip(reason='no way of currently testing this')   #标记为skip后,该用例不会执行
        def test_one(self):
            print("test_one方法执行")
            assert  1==1
    
        def test_two(self):
            print("test_two方法执行")
            assert  'o' in 'love'
    
        def test_three(self):
            print("test_three方法执行")

    运行结果:

    C:Userscalecheckapi est>pytest test_gy.py -v
    =========================================================================================================== test session starts ============================================================================================================
    platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:usersipharmacarepython37python.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
    rootdir: C:Userscalecheckapi est
    plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
    collected 3 items

    test_gy.py::TestClass::test_one SKIPPED [ 33%]
    test_gy.py::TestClass::test_two PASSED [ 66%]
    test_gy.py::TestClass::test_three PASSED [100%]

    ======================================================================================================= 2 passed, 1 skipped in 0.28s =======================================================================================================

    2.pytest.skip(reason):在测试执行或设置期间强制跳过

    import pytest
    
    class TestClass():
        def test_one(self):
            print("test_one方法执行")
            if 1==1:
                pytest.skip("skip") #如果if语句为True时,执行到这里会跳过,方法后面的代码都不会执行
            assert  1==1
    
        def test_two(self):
            print("test_two方法执行")
            assert  'o' in 'love'
    
        def test_three(self):
            print("test_three方法执行")
            assert 3-2==1

    运行结果:

    C:Userscalecheckapi est>pytest test_gy.py -v
    =========================================================================================================== test session starts ============================================================================================================
    platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:usersipharmacarepython37python.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
    rootdir: C:Userscalecheckapi est
    plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
    collected 3 items

    test_gy.py::TestClass::test_one SKIPPED [ 33%]
    test_gy.py::TestClass::test_two PASSED [ 66%]
    test_gy.py::TestClass::test_three PASSED [100%]

    ======================================================================================================= 2 passed, 1 skipped in 0.22s =======================================================================================================

     3.pytest.skip(reason,allow_module_level=True):跳过整个模块

    import pytest

    if 1 == 1: pytest.skip("skip", allow_module_level=True)
    class TestClass(): def test_one(self): print("test_one方法执行") assert 1==1 def test_two(self): print("test_two方法执行") assert 'o' in 'love' def test_three(self): print("test_three方法执行") assert 3-2==1

    运行结果:

    C:Userscalecheckapi est>pytest test_gy.py -v
    =========================================================================================================== test session starts ============================================================================================================
    platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:usersipharmacarepython37python.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
    rootdir: C:Userscalecheckapi est
    plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
    collected 0 items / 1 skipped

    ============================================================================================================ 1 skipped in 0.31s ===================

     skip If跳过用例(有条件跳过)

    1.装饰器:

    @pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")

     

    import pytest
    class TestClass():
        name='one'
        @pytest.mark.skipif(name=='one',reason="skip")   #如果装饰器里面的条件满足会跳过改方法
        def test_one(self):
            print("test_one方法执行")
            assert  1==1
    
        def test_two(self):
            print("test_two方法执行")
            assert  'o' in 'love'
    
    运行结果:
    C:Userscalecheckapi	est>pytest test_gy.py -v
    =========================================================================================================== test session starts ============================================================================================================
    platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:usersipharmacarepython37python.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
    rootdir: C:Userscalecheckapi	est
    plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
    collected 2 items
    
    test_gy.py::TestClass::test_one SKIPPED                                                                                                                                                                                               [ 50%]
    test_gy.py::TestClass::test_two PASSED                                                                                                                                                                                                [100%]
    
    ======================================================================================================= 1 passed, 1 skipped in 0.17s =======================================================================================================

     2.模块之间共享skipif标记

    test_m1.py:在模块中定义一个装饰器name_one

    import pytest
    
    name = 'one'
    name_one = pytest.mark.skipif(name == "one", reason='skip')  #定义装饰器name_one
    
    class TestClass():
        @name_one   #调用装饰器
        def test_one(self):
            print("test_one方法执行")
            assert  1==1
    
        def test_two(self):
            print("test_two方法执行")
            assert  'o' in 'love'

    test_m2.py:导入test_m1.py模块,并调用它的name_one装饰器

    import pytest
    from test_cc.test_m1 import name_one
    
    @name_one    #导入test_gy模块并调用它的装饰器name_one
    def test_three():
            print("test_three方法执行")
            assert  1==1
    
    def test_four():
            print("test_four方法执行")
            assert  'l' in 'liao'

    运行test_m2.py模块的测试用例:

    C:Userscalecheckapi	est_cc>pytest test_m2.py -v
    =========================================================================================================== test session starts ============================================================================================================
    platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:usersipharmacarepython37python.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
    rootdir: C:Userscalecheckapi	est_cc
    plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
    collected 2 items
    
    test_m2.py::test_three SKIPPED                                                                                                                                                                                                        [ 50%]
    test_m2.py::test_four PASSED                                                                                                                                                                                                          [100%]
    
    ======================================================================================================= 1 passed, 1 skipped in 0.14s =======================================================================================================

     3.skipif装饰类:当条件成立时,类中的所有方法都不会执行

    import pytest,sys
    
    @pytest.mark.skipif(sys.platform=='win32',reason='skip')
    class TestClass():
        def test_one(self):
            print("test_one方法执行")
            assert  1==1
    
        def test_two(self):
            print("test_two方法执行")
            assert  'o' in 'love'

    运行结果:

    C:Userscalecheckapi est_cc>pytest test_m1.py -v
    =========================================================================================================== test session starts ============================================================================================================
    platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:usersipharmacarepython37python.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
    rootdir: C:Userscalecheckapi est_cc
    plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
    collected 2 items

    
    

    test_m1.py::TestClass::test_one SKIPPED [ 50%]
    test_m1.py::TestClass::test_two SKIPPED [100%]

    
    

    ============================================================================================================ 2 skipped in 0.13s ============================================================================================================

     
  • 相关阅读:
    解决python中write()函数向文件中写中文时出现乱码的问题
    解决Object() takes on paramenters的问题
    Flexbox
    node.js中的回调
    jQuery Mobile Api
    Sql Server总结
    文件夹和文件、Path类、流、序列化
    C# 字符串
    枚举|标志枚举+|(或)和&(与)运算|类型转换|值类型和引用类型|传参|异常|垃圾回收
    继承、多态、封装、接口
  • 原文地址:https://www.cnblogs.com/pipile/p/12658341.html
Copyright © 2020-2023  润新知