pytest中使用ordering控制函数的执行顺序

目录

1--安装ordering

2--ordering的使用

3---控制函数的执行顺序


安装ordering

  1. pip install pytest-ordering
  2. 出现下面的情况,安装就算是完成了

ordering的使用

  1. 使用

    复制代码
    @pytest.mark.run(order=1)
  2. 格式

    1. @pytest.mark.run(order=顺序)
    2. 这里的数字越大,执行的优先级别越低
    3. 这个是在方法上面使用的
    复制代码
    @pytest.mark.run(order=1)
    def test_demo_002(self):
        print("---------test_demo_002--------")

控制函数的执行顺序

  1. 首先我们编写一下代码

  2. 复制代码
    import pytest
    
    
    class TestOrdering:
    
        def test_demo_001(self):
            print("---------test_demo_001--------")
    
        
        def test_demo_002(self):
            print("---------test_demo_002--------")
    
        
        def test_demo_003(self):
            print("---------test_demo_003--------")
    
        def test_demo_004(self):
            print("---------test_demo_004--------")
    
        def test_demo_005(self):
            print("---------test_demo_005--------")
    
        def test_demo_006(self):
            print("---------test_demo_006--------")
  3. 我们可以看一下默认的执行顺序

  4. 使用

    复制代码
    @pytest.mark.run(order=1)
    控制函数的执行顺序  6->5->4->3->2->1
  5. 修改代码

    复制代码
    import pytest
    
    
    class TestOrdering:
        @pytest.mark.run(order=6)
        def test_demo_001(self):
            print("---------test_demo_001--------")
    
        @pytest.mark.run(order=5)
        def test_demo_002(self):
            print("---------test_demo_002--------")
    
        @pytest.mark.run(order=4)
    
        def test_demo_003(self):
            print("---------test_demo_003--------")
    
        @pytest.mark.run(order=3)
        def test_demo_004(self):
            print("---------test_demo_004--------")
    
        @pytest.mark.run(order=2)
        def test_demo_005(self):
            print("---------test_demo_005--------")
    
        @pytest.mark.run(order=1)
        def test_demo_006(self):
            print("---------test_demo_006--------")
  6. 查看运行结果

    1. 可以看到函数的执行结果已经被修改了
    2. 通过 @pytest.mark.run(order=数字)
      1. 成功的控制了函数的执行顺序
相关推荐
岁月宁静2 分钟前
三、《从零手撸 Agent》 · system prompt 与核心参数:调好你的旋钮
后端·python·agent
吴声子夜歌4 分钟前
ApacheCommons——commons-lang3(Java 基础语言增强)(二)
java·开发语言·apache
卷无止境6 分钟前
除了写代码,AI智能体还能帮开发者做什么
人工智能·python
传奇开心果编程7 分钟前
【Rust入门知识点学与练】第1课变量与打印
开发语言·学习·rust
我不会起名字32211 分钟前
一天一道算法题(26):栈的简单应用
java·数据结构·python·算法·leetcode·golang·
爱吃苹果的梨叔14 分钟前
AI 算力监控中心怎么建?分布式坐席 + 大屏联动 + 过程回放
人工智能·分布式·python
JarmanYuo15 分钟前
YOLO 涨点研究(六):网络结构改进之小目标增强篇1——无人机视角下的车辆与行人检测
人工智能·pytorch·python·yolo·计算机视觉·无人机
2601_9622972520 分钟前
python自带缓存lru_cache用法及扩展的使用_python
python·缓存·装饰器·lru_cache·my_cache
招财小梗25 分钟前
沈阳AI企业咨询可定制数字化方案吗?
大数据·人工智能·python