pytest简易教程(03):pytest固件、及用例执行顺序
pytest简易教程汇总,详见:https://www.cnblogs.com/uncleyong/p/17982846
固件分类
固件用于执行前的初始化参数、执行后的清理动作。
类型 | 规则 |
setup_module/teardown_module |
全局模块级 模块运行前/后运行(只运行一次) |
setup_function/teardown_function |
函数级 每个函数用例运行前/后运行 |
setup_class/teardown_class |
类级 每个class运行前/后运行(只运行一次) |
setup_method(setup)/teardown_method(teardown) |
方法级 类中每个方法用例执行前/后运行,setup_method和setup、teardown_method和teardown二选一即可 |
演示
示例:一个module,两个函数,两个类,每个类两个方法
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ def setup_module(): print ( "初始化:setup_module" ) def teardown_module(): print ( "清理:teardown_module" ) def setup_function(): print ( "初始化:setup_function" ) def teardown_function(): print ( "清理:teardown_function" ) def test_f(): print ( "--------------test_f" ) class Test01: def setup_class( self ): print ( "初始化:setup_class1" ) def teardown_class( self ): print ( "清理:teardown_class1" ) def setup_method( self ): print ( "初始化:setup_method1" ) def teardown_method( self ): print ( "清理:teardown_method1" ) def test_c( self ): print ( "--------------test_c" ) def test_d( self ): print ( "--------------test_d" ) class Test02: def setup_class( self ): print ( "初始化:setup_class2" ) def teardown_class( self ): print ( "清理:teardown_class2" ) def setup_method( self ): print ( "初始化:setup_method2" ) def teardown_method( self ): print ( "清理:teardown_method2" ) def test_a( self ): print ( "--------------test_a" ) def test_b( self ): print ( "--------------test_b" ) def test_e(): print ( "--------------test_e" ) |
结果:通过结果可以看到,固件执行规则和我们最开始描述的一致
用例执行顺序
示例:
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ def setup_module(): print ( "初始化:setup_module" ) def teardown_module(): print ( "清理:teardown_module" ) def setup_function(): print ( "初始化:setup_function" ) def teardown_function(): print ( "清理:teardown_function" ) def test_f(): print ( "--------------test_f" ) class Test01: def setup_class( self ): print ( "初始化:setup_class1" ) def teardown_class( self ): print ( "清理:teardown_class1" ) def setup_method( self ): print ( "初始化:setup_method1" ) def teardown_method( self ): print ( "清理:teardown_method1" ) def test_d( self ): print ( "--------------test_d" ) def test_c( self ): print ( "--------------test_c" ) class Test02: def setup_class( self ): print ( "初始化:setup_class2" ) def teardown_class( self ): print ( "清理:teardown_class2" ) def setup_method( self ): print ( "初始化:setup_method2" ) def teardown_method( self ): print ( "清理:teardown_method2" ) def test_b( self ): print ( "--------------test_b" ) def test_a( self ): print ( "--------------test_a" ) def test_e(): print ( "--------------test_e" ) |
结论:pytest框架默认根据书写代码的先后顺序来执行
__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中提取指定内容