8. Pytest跳过某个测试用例:skip和skipif

一、前言

  • skip和skipif,看名字就是跳过测试的意思,主要用于不想执行的代码,标记后,标记的代码不执行。
  • 希望满足某些条件才执行某些测试用例,否则pytest会跳过运行该测试用例
  • 实际常见场景:根据平台不同执行测试、跳过依赖、功能未完成预期不能执行的测试

二、学习目标

1.@pytest.mark.skip跳过执行测试函数

2.@pytest.mark.skipif若满足条件,则跳过测试函数

3.自定义@pytest.mark.skip()标签

4.通过pytest.skip方法跳过测试函数

5.跳过测试类

6.跳过模块

7.pytest.importorskip模块导入失败则跳过

8.跳过指定文件或目录

三、知识点

1.【skip跳过执行测试函数】

  • 语法:

    @pytest.mark.skip(reason=None) #reason非必填,自定义跳过的原因
    
  • 代码示例:

    #测试用例文件test_XXX.py
    import pytest
    
    @pytest.mark.skip(reason="no reason") #直接作用在测试用例的上方
    def test_01():
        print("---用例1执行---")
    
    
    class TestCase():
        @pytest.mark.skip(reason="no reason")
        def test_02(self):
            print("---用例2执行---")
    
        def test_03(self):
            print("---用例3执行---")
    
  • 运行效果:

    test-demo7.py::test_01 SKIPPED                                           [ 33%]
    Skipped: no reason #跳过的原因
    
    test-demo7.py::TestCase::test_02 SKIPPED                                 [ 66%]
    Skipped: no reason
    
    test-demo7.py::TestCase::test_03 PASSED                                  [100%]---用例3执行---
    ======================== 1 passed, 2 skipped in 0.02s =========================
    

2.【skipif若满足条件,则跳过测试函数】

  • 语法:

    @pytest.mark.skipif(condition, reason=None) #condition为一个表达式,表达式成立则跳过
    #注:如果多个标签一起使用,满足其中一个跳过条件则会跳过该测试函数。
    
  • 代码示例:

    import pytest
    
    
    def test_01():
        print("---用例1执行---")
    
    
    class TestCase():
        # 当多个@pytest.mark.skipif()标签时,若满足一个,则跳过测试函数
        @pytest.mark.skipif(condition=1 > 2, reason="no reason")
        @pytest.mark.skipif(condition=1 < 2, reason="no reason")
        def test_02(self):
            print("---用例2执行---")
    
        def test_03(self):
            print("---用例3执行---")
    
  • 运行效果:

    test-demo7.py::test_01 PASSED                                            [ 33%]---用例1执行---
    
    test-demo7.py::TestCase::test_02 SKIPPED                                 [ 66%]
    Skipped: no reason  #跳过的原因
    
    test-demo7.py::TestCase::test_03 PASSED                                  [100%]---用例3执行---
    ======================== 2 passed, 1 skipped in 0.02s =========================
    

3.【自定义@pytest.mark.skip()标签】

  • 语法:

    myskip = pytest.mark.skip() 或 myskip = pytest.mark.skipif(condition=...)
    
    #装饰时用该变量代替标签即可:@myskip
    
  • 代码示例:

    import pytest
    
    # myskip = pytest.mark.skip()
    myskip = pytest.mark.skipif(condition=2 > 1, reason="no reason")
    
    @myskip
    def test_01():
        print("---用例1执行---")
    
    class TestCase():
    
        @myskip
        def test_02(self):
            print("---用例2执行---")
    
        def test_03(self):
            print("---用例3执行---")
    
  • 运行效果:

    test-demo7.py::test_01 SKIPPED                                           [ 33%]
    Skipped: no reason
    
    test-demo7.py::TestCase::test_02 SKIPPED                                 [ 66%]
    Skipped: no reason
    
    test-demo7.py::TestCase::test_03 PASSED                                  [100%]---用例3执行---
    
    ======================== 1 passed, 2 skipped in 0.02s =========================
    

4.【通过pytest.skip()方法跳过测试函数】

  • 语法:

    pytest.skip(msg="no reason") #写在用例函数内,msg非必填
    
  • 代码示例:

    import pytest
    
    def test_01():
        pytest.skip(msg="no reason")
        print("---用例1执行---")
    
    class TestCase():
    
        def test_02(self):
            pytest.skip()
            print("---用例2执行---")
    
        def test_03(self):
            print("---用例3执行---")
    
  • 运行效果:

    test-demo7.py::test_01 SKIPPED                                           [ 33%]
    Skipped: no reason
    
    test-demo7.py::TestCase::test_02 SKIPPED                                 [ 66%]
    Skipped: <Skipped instance>
    
    test-demo7.py::TestCase::test_03 PASSED                                  [100%]---用例3执行---
    ======================== 1 passed, 2 skipped in 0.02s =========================
    

5.【跳过测试类】

  • 语法:

    @pytest.mark.skip()和@pytest.mark.skipif()
    #跳过测试类其实和跳过测试方法一样,用他们装饰测试类就好啦。
    
  • 代码示例:

    import pytest
    myskip = pytest.mark.skip(reason="no reason")
    
    def test_01():
      print("---用例1执行---")
    
    @myskip
    class TestCase():
    
      def test_02(self):
        print("---用例2执行---")
    
      def test_03(self):
        print("---用例3执行---")
    
  • 运行效果:

    test-demo7.py::test_01 PASSED                                            [ 33%]---用例1执行---
    
    test-demo7.py::TestCase::test_02 SKIPPED                                 [ 66%]
    Skipped: no reason
    
    test-demo7.py::TestCase::test_03 SKIPPED                                 [100%]
    Skipped: no reason
    ======================== 1 passed, 2 skipped in 0.02s =========================
    

6.【跳过模块】

  • 语法:

    pytestmark = pytest.mark.skip(condition, reason=None) 
    #注:pytestmark不能改名
    
  • 代码示例:

    import pytest
    
    pytestmark = pytest.mark.skip(condition=2 > 1, reason='no reason') #pytestmark不能改名
    
    def test_01():
        print("---用例1执行---")
    
    
    class TestCase():
    
        def test_02(self):
            print("---用例2执行---")
    
        def test_03(self):
            print("---用例3执行---")
    
  • 运行效果:

    test-demo7.py::test_01 SKIPPED                                           [ 33%]
    Skipped: no reason
    
    test-demo7.py::TestCase::test_02 SKIPPED                                 [ 66%]
    Skipped: no reason
    
    test-demo7.py::TestCase::test_03 SKIPPED                                 [100%]
    Skipped: no reason
    ============================= 3 skipped in 0.02s ==============================
    

7.【pytest.importorskip模块导入失败则跳过】

  • 语法:

    #当引入某个模块失败时,我们同样可以跳过后续部分的执行;
    docutils = pytest.importorskip("docutils")
    #我们也可以为其指定一个最低满足要求的版本,判断的依据是检查引入模块的__version__属性:
    docutils = pytest.importorskip("docutils", minversion="0.3") 
    
  • 代码示例:

    import pytest
    
    #我们也可以为其指定一个最低满足要求的版本,判断的依据是检查引入模块的__version__属性:
    docutils = pytest.importorskip("docutils", minversion="0.3")
    
    def test_01():
        print("---用例1执行---")
    
    class TestCase():
    
        def test_02(self):
            print("---用例2执行---")
    
        def test_03(self):
            print("---用例3执行---")
    
  • 运行效果:

    Skipped: could not import 'docutils': No module named 'docutils'
    collected 0 items / 1 skipped
    
    ============================= 1 skipped in 0.03s ==============================
    

8.【跳过指定文件或目录】

通过在conftest.py中配置collect_ignore_glob项,可以在用例的收集阶段跳过指定的文件和目录;

例如,跳过当前测试目录中文件名匹配test_*.py规则的文件和config的子文件夹sub中的文件:

collect_ignore_glob = ['test*.py', 'config/sub']
posted @ 2023-01-17 10:27  测开星辰  阅读(245)  评论(0编辑  收藏  举报