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. 成功的控制了函数的执行顺序
相关推荐
一个天蝎座 白勺 程序猿5 小时前
Python爬虫(47)Python异步爬虫与K8S弹性伸缩:构建百万级并发数据采集引擎
爬虫·python·kubernetes
XiaoMu_0016 小时前
基于Django+Vue3+YOLO的智能气象检测系统
python·yolo·django
honder试试7 小时前
焊接自动化测试平台图像处理分析-模型训练推理
开发语言·python
^Rocky7 小时前
JavaScript性能优化实战
开发语言·javascript·性能优化
心本无晴.7 小时前
Python进程,线程
python·进程
ponnylv7 小时前
深入剖析Spring Boot启动流程
java·开发语言·spring boot·spring
萧邀人7 小时前
第一课、Cocos Creator 3.8 安装与配置
开发语言
Jayden_Ruan8 小时前
C++逆向输出一个字符串(三)
开发语言·c++·算法
不吃鱼的羊8 小时前
启动文件Startup_vle.c
c语言·开发语言
VBA63379 小时前
VBA之Word应用第四章第二节:段落集合Paragraphs对象(二)
开发语言