【pytest学习6】fixture的作用范围
fixture可以代替setup和teardown,怎么在不同的场景下进行使用运行呢?比如我只想要启动浏览器一次呢?如果每个用例按照前面的都加入fixture那么每条用例都会运行,其实fixture中有参数可以进行配置,配置后可以在不同的场景下进行使用,这里就要引入新的知识fixture的作用范围。
fixture作用范围
fixture中的scope参数可以进行配置,我们可以从源码中看到scope的默认参数为function。下面也介绍了scope中的其他参数:“class”,“module”,“session”
def fixture( # noqa: F811 fixture_function: Optional[_FixtureFunction] = None, *, scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function", params: Optional[Iterable[object]] = None, autouse: bool = False, ids: Optional[ Union[ Iterable[Union[None, str, float, int, bool]], Callable[[Any], Optional[object]], ] ] = None, name: Optional[str] = None ) -> Union[FixtureFunctionMarker, _FixtureFunction]: """用于标识夹具工厂功能的装饰器。 该装饰器可以使用,也可以不使用参数,来定义 夹具的功能。 fixture函数的名称稍后可以被引用以导致its 在运行测试之前调用:测试模块或类可以使用 ``pytest.mark.usefixtures(fixturename)`` marker. 测试函数可以直接使用fixture名称作为其中的输入参数 从fixture函数返回的fixture实例的情况 注入。 :param scope: The scope for which this fixture is shared; one of ``"function"`` (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``.
function
参数scope默认才是就是function,主要应用单个测试函数中,yield前面的代码在测试用例运行前执行,后面的代码在测试用例运行之后运行。未添加到的测试函数,不进行执行
# test_01.py import pytest @pytest.fixture(scope='function') def login(): print('输入用户名,密码,完成登录!') yield print('关闭浏览器!') class Test_01: def test_01(self, login): print('需要用到登录功能') print('----用例01---') def test_02(self): print('不需要登录功能') print('----用例02---') def test_03(self, login): print('需要用到登录功能') print('----用例03---') if __name__ == '__main__': pytest.main(['-s', 'test__01.py'])
module
scope值为“module”表示为模块级别的fixture每个模块之运行1次,无论模块中多少个测试用例,都只运行一次。
# test_01.py import pytest @pytest.fixture(scope='module') def login(): print('输入用户名,密码,完成登录!') yield print('关闭浏览器!') class Test_01: def test_01(self, login): print('需要用到登录功能') print('----用例01---') def test_02(self): print('不需要登录功能') print('----用例02---') def test_03(self, login): print('需要用到登录功能') print('----用例03---') if __name__ == '__main__': pytest.main(['-s', 'test__01.py'])
class
scope的值为“class”表示类机会的fixture中每个测试函数都只执行一次。无论模块中多少个用例,都只执行一次。
# test_01.py import pytest @pytest.fixture(scope='class') def login(): print('输入用户名,密码,完成登录!') yield print('关闭浏览器!') class Test_01: def test_01(self, login): print('需要用到登录功能') print('----用例01---') def test_02(self): print('不需要登录功能') print('----用例02---') def test_03(self, login): print('需要用到登录功能') print('----用例03---') if __name__ == '__main__': pytest.main(['-s', 'test__01.py'])
session
scope的值为“session”表示会话级别的fixture。每次会话都会只执行一次。每个添加的用例只会执行1次fixture
# test__01.py import pytest @pytest.fixture(scope='session') def login(): print('输入用户名,密码,完成登录!') yield print('关闭浏览器!') class Test_01: def test_01(self, login): print('需要用到登录功能') print('----用例01---') def test_02(self): print('不需要登录功能') print('----用例02---') def test_03(self, login): print('需要用到登录功能') print('----用例03---') if __name__ == '__main__': pytest.main(['-s', 'test__01.py'])
执行顺序
了解了fixture中scope参数的各个使用范围,那么如果同时使用,这些执行顺序是什么样子的呢?实践证明下
# test__01.py import pytest @pytest.fixture(scope='session') def fix_session(): print('这是属于fixture中的会话级别') yield print('session') @pytest.fixture(scope='class') def fix_class(): print('这是属于fixture中的类级别') yield print('session') @pytest.fixture(scope='module') def fix_module(): print('这是属于fixture中的模块级别') yield print('module') @pytest.fixture() def fix_function(): print('这是属于fixture中的函数级别') yield print('function') class Test_01: def test_01(self, fix_function, fix_class,fix_module,fix_session): print('----用例01---') if __name__ == '__main__': pytest.main(['-s', 'test__01.py'])
通过上面实践发现执行顺序:session>>module>>class>>function
声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/
如出现转载未声明 将追究法律责任~谢谢合作
分类:
python-pytest
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具