pytest简易教程(07):fixture跨模块共享(conftest.py)
pytest简易教程汇总,详见:https://www.cnblogs.com/uncleyong/p/17982846
关于conftest.py
如果多个模块使用的fixture相同,那么,我们可以将fixture写在conftest.py中(通过conftest.py管理共享的fixture),这样达到跨模块和文件的效果
conftest的特点:
1、文件名称默认为conftest.py,是pytest里面固定的名字,不能随意更改,通常在里面写用例执行前的一些初始化操作
2、conftest.py文件可以有多个(全局、局部),搜索优先级自底而上(从和模块同级目录开始找,一直到项目根目录),遵循就近原则
3、conftest.py中的fixture可以跨文件调用,支持函数引用、通过装饰器调用,也可以自动适配(此时autouse要改为True;如果就近的一个都是False,远的一个都是True,此时还是会自动适配近的,详见文末示例)
4、conftest.py文件作用范围是它同级test文件,或者下面的test文件
5、不需要import导入conftest.py,pytest用例会自动识别该文件,放到项目的根目录下就可以全局目录调用
6、conftest.py文件不能被其他文件导入
示例
仅局部conftest.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import pytest @pytest.fixture() def login(): print( "---登录" ) @pytest.fixture() def fun(login): print( "---fun" ) |
test_qzcsbj.py
1 2 3 4 5 6 7 8 9 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import pytest def test_a(fun): print( "--------------test_a" ) |
结果:
添加全局conftest.py
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 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import pytest @pytest.fixture(autouse=True, scope= "function" ) def f(): print( "---fixture : function-前" ) yield print( "---fixture : function-后" ) @pytest.fixture(autouse=True, scope= "class" ) def f2(): print( "---fixture : class-前" ) yield print( "---fixture : class-后" ) @pytest.fixture(autouse=True, scope= "module" ) def f3(): print( "---fixture : module-前" ) yield print( "---fixture : module-后" ) @pytest.fixture(autouse=True, scope= "package" ) def f4(): print( "---fixture : package-前" ) yield print( "---fixture : package-后" ) @pytest.fixture(autouse=True, scope= "session" ) def f5(): print( "---fixture : session-前" ) yield print( "---fixture : session-后" ) |
结果:
全局和局部有同名的fixture
全局添加:
1 2 3 4 5 | @pytest.fixture(autouse=True, scope= "function" ) def f0(): print( "---fixture : function-前(全局)" ) yield print( "---fixture : function-后(全局)" ) |
局部添加:
1 2 3 4 5 | @pytest.fixture(autouse=False, scope= "function" ) def f0(): print( "---fixture : function-前(局部)" ) yield print( "---fixture : function-后(局部)" ) |
结果:调用的fixture是局部的f0,虽然是False,但是全局是True,所以哪怕没调用,也会执行这个局部fixture
如果
全局添加:
1 2 3 4 5 | @pytest.fixture(autouse=False, scope= "function" ) def f0(): print( "---fixture : function-前(全局)" ) yield print( "---fixture : function-后(全局)" ) |
局部添加:
1 2 3 4 5 | @pytest.fixture(autouse=True, scope= "function" ) def f0(): print( "---fixture : function-前(局部)" ) yield print( "---fixture : function-后(局部)" ) |
结果:
指定引用全局fixture
用例中可以通过@pytest.mark.usefixtures()指定引用全局autouse=False的fixture
__EOF__

关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(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中提取指定内容