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. 成功的控制了函数的执行顺序
相关推荐
brent4232 分钟前
DAY49 预训练模型
python
非凡ghost15 分钟前
MPC-QT视频播放器(基于Qt框架播放器)
开发语言·windows·qt·音视频·软件需求
转基因16 分钟前
C++的IO流
开发语言·c++
一碗绿豆汤18 分钟前
Java语言概述和开发环境-1
java·开发语言
愈努力俞幸运22 分钟前
rust安装
开发语言·后端·rust
清水白石00831 分钟前
深入 Python 的底层世界:从 C 扩展到 ctypes 与 Cython 的本质差异全解析
c语言·python·neo4j
Amelia11111139 分钟前
day49
python
天天进步20151 小时前
【Nanobrowser源码分析4】交互篇: 从指令到动作:模拟点击、滚动与输入的底层实现
开发语言·javascript·ecmascript
console.log('npc')1 小时前
vue2中子组件父组件的修改参数
开发语言·前端·javascript
码点1 小时前
【无标题】日文字库Japan.ini
开发语言