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. 成功的控制了函数的执行顺序
相关推荐
左左右右左右摇晃21 小时前
手写Tomcat原理整理
java·开发语言·笔记·tomcat
孫治AllenSun21 小时前
【JVM】四大引用类型分析
java·开发语言·jvm
看-是灰机21 小时前
使用go语言实现对接
linux·开发语言·后端·docker·语言模型·golang·飞书
xing-xing21 小时前
魔搭社区(ModelScope)下载模型文件
python·jupyter·llm
一孤程1 天前
Pytest+Selenium搭建自动化框架-保姆级实战教程
selenium·自动化·pytest
承渊政道1 天前
【Python学习】(了解使用库、标准库、第三方库以及综合案例实操)
python·学习·pycharm·标准库·第三方库·使用库·综合案例
草莓熊Lotso1 天前
【LangChain】输出解析器全解:让大模型输出从 “聊天” 变 “机器可读”
服务器·数据库·python·langchain·pip
卷无止境1 天前
Python 脚本工程化:从"能跑就行"到"生产级代码"
后端·python
卷无止境1 天前
Python 函数式编程:从思想到实践
后端·python
人间凡尔赛1 天前
Next.js 16 生产级实战:Cache Components + View Transitions 完整指南
开发语言·javascript·ecmascript