pytest中使用skip跳过某个函数

目录

1-- 如果我们在pytets中不想让某些函数执行

2-- 跳过某些我们不想执行的函数


如果我们在pytets中不想让某些函数执行

  1. 因为这些函数执行会因为某些原因执行错误
    1. 可能因为,版本不兼容
    2. 机型原因等
  2. 想跳过当前执行
    1. 可以通过
      1.

      复制代码
      @pytest.mark.skip(reason ="当前版本不兼容")
      1. @pytest.mark.skip(reason = "原因") 直接跳过

跳过某些我们不想执行的函数

  1. 复制代码
    import pytest
    
    
    class TestRerunFailures:
    
        def test_demo_001(self):
            print("---------test_demo_001--------")
            print("success")
            assert True
    
        def test_demo_002(self):
            print("---------test_demo_002--------")
            print("success")
            assert True
    
        
       def test_demo_003(self):
            print("---------test_demo_003--------")
            print("fail")
            assert False
        
    def test_demo_004(self):        print("---------test_demo_004--------")
            print("fail")
            assert False
    
        def test_demo_005(self):
            print("---------test_demo_005--------")
            print("success")
            assert True
  2. 我们不想执行

    1. def test_demo_003(self)
    2. def test_demo_004(self)
  3. 使用
    1.

    复制代码
    @pytest.mark.skip(reason="当前版本不兼容")
    def test_demo_003(self):
        print("---------test_demo_003--------")
        print("fail")
        assert False
    复制代码
    1. 复制代码
      @pytest.mark.skip(reason="测试设备不支持")
      def test_demo_004(self):
          print("---------test_demo_004--------")
          print("fail")
          assert False
      复制代码
    2. 标记完之后的运行结果

    3. 在执行已经标记过的函数已经跳过了

  4. 也可以使用 skipif进行跳过
    1.

    复制代码
    @pytest.mark.skipif(condition=False, reason="fail")
    def test_demo_002(self):
        print("---------test_demo_002--------")
        print("success")
        assert True
    复制代码
    @pytest.mark.skipif(condition=False, reason="fail")
    1. 这个可以针对condition条件机型判断
相关推荐
OPEN-F9 小时前
C++综合实战:面向对象图书管理系统
开发语言·c++
ttwuai9 小时前
Go 后台清空操作日志失败,权限和无 WHERE 删除怎么排查?
开发语言·后端·golang
baopixiaoz9 小时前
BeeQuant × BeeAgent:AI量化新范式
大数据·人工智能·python·区块链
西西弗Sisyphus9 小时前
Qt 在无边框窗口上做一套换肤系统
开发语言·数据库·qt
西西弗Sisyphus9 小时前
Qt 启动 UsageStatistic 插件报错
开发语言·qt
NeoGressAI外贸数字化10 小时前
外贸独立站零询盘排查:从 Google Search Console 到 PageSpeed 的技术实操
开发语言·c++
IT毕设实战小研10 小时前
基于大数据的AI应用对就业与收入增长的区域差异分析及可视化
大数据·人工智能·python·随机森林·机器学习·课程设计
无小道10 小时前
C/C++——异步编程小记
开发语言·c++·c++11
奇树谦10 小时前
Pimpl 模式(d-pointer)详解:如何解决 C++ 头文件过大、编译依赖和 ABI 兼容问题
开发语言·c++
摆烂老大10 小时前
论文学习 | Expert Systems With Applications2026 | 可微分建模:S4D-HBV
python·学习·水文