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=负数
__EOF__

本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2022-02-23 答疑记录:jmeter从返回的html中提取指定内容