首页 > 其他分享 >8. Pytest跳过某个测试用例:skip和skipif

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

时间:2023-01-17 10:34:11浏览次数:36  
标签:skip skipif py pytest --- 用例 reason Pytest test

一、前言

  • 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']

标签:skip,skipif,py,pytest,---,用例,reason,Pytest,test
From: https://www.cnblogs.com/ckxingchen/p/17057174.html

相关文章

  • 11. Pytest常用插件:pytest-ordering调整用例执行顺序
    一、前言在pytest中,测试用例的默认执行顺序是从上到下执行的,但是有时候我们会有这样的需求,就是打乱测试用例的执行顺序来达到某个测试效果,这时候就需要用到Pytest中的一个......
  • 10. Pytest设置用例标签:mark
    一、前言mark主要用来标记用例,通过不同的标记实现不同的运行策略。一个大项目自动化用例时,可以划分多个模块,也可以使用标记功能,标明哪些是模块1用例,哪些是模块2的,运行代码......
  • 13. Pytest常用插件:pytest-repeat重复运行用例
    一、前言上面我们介绍了当用例失败时的重复运行,其实我们在实际工作中还会遇到一种情况,我们就是单纯的想让某条用例重复运行指定的次数。平常在做功能测试的时候,经常会遇......
  • 12. Pytest常用插件: pytest-rerunfailures失败用例重跑
    一、前言测试环境不稳定偶发接口超时(和服务无关,纯粹是环境问题),然后执行接口case也因此偶发失败。比如同一个接口case跑五次,其中有两次失败,另外三次都是成功的,这种偶发性的......
  • 执行docker-compose up -d时出现ERROR: Failed to Setup IP tables: Unable to enable
    原因是因为防火墙关闭之后需要重启docker服务。执行:servicedockerrestart即可。......
  • pytest--前置后置执行
    前言在unittest中就有前置setup和后置teardown来处理测试用例执行前的准备工作(浏览器驱动实例化,数据库连接等)以及执行后的处理工作(清理数据,关闭浏览器驱动,关闭数据库连接......
  • Pytest - 跳过指定文件或者目录
    通过在conftest.py文件中加入collect_ignore_glob项,可以在用例的收集阶段跳过指定的文件和目录#conftest.py文件中加入collect_ignore_glob配置:collect_ignore_glob......
  • Selenium64-pytest.ini
    配置文件pytest.inipytest.ini是什么?pytest.ini是pytest的主配置文件,可以改变pytest的默认行为,有很多可配置的选项。在执行文件根目录配置pytest.ini文件。日志的作用:我们......
  • continue跳過循環(skippaart程序),接受設定的合法分數來進行平均分求值,并展現最高分,最低
    1#include<stdio.h>2intmain()3{4constfloatMIN=0.0f;//分數下限是0分5constfloatMAX=100.0f;//分數上限是100分67floa......
  • pytest测试使用
    固件:pytest.fixture()定义固件pytest会在执行测试函数之前(或之后)加载运行它们,最常见的可能就是数据库的初始连接和最后关闭操作标记:pytest.mark.***pytest会在执行......