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. 成功的控制了函数的执行顺序
相关推荐
2401_894241923 分钟前
机器学习与人工智能
jvm·数据库·python
vx-程序开发9 分钟前
springboot在线装修管理系统-计算机毕业设计源码56278
java·c语言·spring boot·python·spring·django·php
Dxy123931021612 分钟前
js如何把字符串转数字
开发语言·前端·javascript
进击的雷神25 分钟前
主办方过滤、展位号模糊提取、多层级官网爬取、缅文编码解码——缅甸塑料展爬虫四大技术难关攻克纪实
网络·爬虫·python
_饭团30 分钟前
字符串函数全解析:12 种核心函数的使用与底层模拟实现
c语言·开发语言·学习·考研·面试·蓝桥杯
Larry_Yanan31 分钟前
Qt网络开发之基于 QWebEngine 实现简易内嵌浏览器
linux·开发语言·网络·c++·笔记·qt·学习
winfredzhang32 分钟前
用 Python + DeepSeek AI 构建文件批量重命名与智能管理工具
python·api·重命名·预览·解压·deepseek
2401_8318249637 分钟前
嵌入式C++驱动开发
开发语言·c++·算法
西西弗Sisyphus38 分钟前
Python 中__pycache__文件夹
python
qingcyb44 分钟前
重复 id 对应的多个对象
开发语言·python