遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

pytest简易教程(29):pytest常用插件 - 控制函数执行顺序(pytest-ordering)

 

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

 

应用场景

用例执行顺序,默认是按照从上到下的顺序进行执行的,详见:https://www.cnblogs.com/uncleyong/p/17956862

如果想自定义执行顺序,也就是改变执行优先级,那么可以使用pytest-ordering

 

插件安装

pip install pytest-ordering

 

使用方式

标记于被测试函数、方法、类:@pytest.mark.run(order=x),根据order值来决定运行顺序

x的值可以是正数、负数、0

 

order值全为正数或者负数,值越小,优先级越高

示例:order都为正数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
 
import pytest
 
class Test01:
    @pytest.mark.run(order=1)
    def test_c(self):
        print("---test_c")
 
    @pytest.mark.run(order=3)
    def test_b(self):
        print("---test_b")
 
    @pytest.mark.run(order=2)
    def test_a(self):
        print("---test_a")

  

结果:

 

示例:order都为负数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
 
import pytest
 
 
class Test01:
    @pytest.mark.run(order=-3)
    def test_c(self):
        print("---test_c")
 
    @pytest.mark.run(order=-1)
    def test_b(self):
        print("---test_b")
 
    @pytest.mark.run(order=-2)
    def test_a(self):
        print("---test_a")

  

结果:

 

优先级:order=0 > order=正数 > 无order > order=负数

示例:同时存在正数、负数、0、无装饰器的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
 
import pytest
 
class Test01:
 
    def test_d(self):
        print("---test_d")
 
    @pytest.mark.run(order=-3)
    def test_c(self):
        print("---test_c")
 
    @pytest.mark.run(order=0)
    def test_b(self):
        print("---test_b")
 
    @pytest.mark.run(order=1)
    def test_a(self):
        print("---test_a")

  

结果:

 

补充:修饰测试类

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
 
import pytest
 
@pytest.mark.run(order=-1)
class Test04:
    def test_d2(self):
        print("---test_d2")
    def test_d(self):
        print("---test_d")
class Test03:
    def test_c2(self):
        print("---test_c2")
    def test_c(self):
        print("---test_c")
@pytest.mark.run(order=1)
class Test02:
    def test_a2(self):
        print("---test_a2")
    def test_a(self):
        print("---test_a")
 
@pytest.mark.run(order=0)
class Test01:
    def test_b2(self):
        print("---test_b2")
    def test_b(self):
        print("---test_b")

  

结果:优先级也是order=0 > order=正数 > 无order > order=负数

 

posted @   全栈测试笔记  阅读(151)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2022-02-23 答疑记录:jmeter从返回的html中提取指定内容
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示