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条件机型判断
相关推荐
biter down5 小时前
14:pytest-order 插件 顺序控制案例
开发语言·python·pytest
郝学胜-神的一滴5 小时前
Qt 高级开发 009: C++ Lambda 表达式
开发语言·c++·qt·软件构建
测试开发-学习笔记5 小时前
从0开始搭建自动化(一)-appium+python
python·自动化
㳺三才人子5 小时前
初探 Flask
后端·python·flask·html
星栈独行5 小时前
我在 Rust 全栈项目里用 JWT 做无状态认证
开发语言·后端·rust·前端框架·开源·github·web
石山代码6 小时前
C++ 轻量级日志系统
开发语言·c++
AI算法沐枫6 小时前
机器学习到底是什么?
人工智能·python·深度学习·机器学习·数据挖掘·大模型·#ai
小技与小术6 小时前
玩转Flask
开发语言·python·flask
SilentSamsara7 小时前
Python 性能优化:tracemalloc、profiling 与 C 扩展加速
开发语言·python·青少年编程·性能优化
冰小忆7 小时前
大驼峰命名规范和小驼峰命名规范的区别是什么?
开发语言·python