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条件机型判断
相关推荐
花酒锄作田2 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪6 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽7 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战7 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋13 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama