pytest中fixture的scope

1|0一. 定义


pytest的fixture中有一个参数scope,它的作用域有五个,分别是:function、class、module、和session

function:每个方法开始之前都会调用一次,方法级别

class:每个类开始之前都会调用一次,类级别

module:每个模块(py文件)开始之前都会调用一次,模块级别

session:一个session(多个模块)开始之前都会调用一次,包级别

2|0二. scope="function"


定义一个test_function_scope.py,写入如下代码并运行:

import pytest @pytest.fixture(scope="function") def function_scope(): print("每个方法开始之前运行一次") msg = "测试function_scope" return msg def test_function_scope_1(function_scope): assert function_scope == "测试function_scope" def test_function_scope_2(function_scope): assert function_scope == "测试function_scope"
#运行结果 ============================= test session starts ============================= platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- D:\program\Python37\python.exe cachedir: .pytest_cache rootdir: E:\virtual_workshop\pytest-demo\test_demo collecting ... collected 2 items test_function_scope.py::test_function_scope_1 每个方法开始之前运行一次 PASSED [ 50%] test_function_scope.py::test_function_scope_2 每个方法开始之前运行一次 PASSED [100%] ============================== 2 passed in 0.03s ==============================

3|0三. scope="class"


定义一个test_class_scope.py,写入如下代码并运行:

import pytest @pytest.fixture(scope="class") def class_scope(): print("每个类开始之前运行一次") msg = "测试class_scope" return msg class TestClassScope1(): def test_class_scope_1(self, class_scope): assert class_scope == "测试class_scope" class TestClassScope2(): def test_class_scope_2(self, class_scope): assert class_scope == "测试class_scope" #运行结果 ============================= test session starts ============================= platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- D:\program\Python37\python.exe cachedir: .pytest_cache rootdir: E:\virtual_workshop\pytest-demo\test_demo collecting ... collected 2 items test_class_scope.py::TestClassScope1::test_class_scope_1 每个类开始之前运行一次 PASSED [ 50%] test_class_scope.py::TestClassScope2::test_class_scope_2 每个类开始之前运行一次 PASSED [100%] ============================== 2 passed in 0.03s ==============================

4|0四. scope="module"


定义一个test_module_scope.py,写入如下代码并运行:

import pytest @pytest.fixture(scope="module") def module_scope(): print("每个模块开始之前运行一次") msg = "测试module_scope" return msg class TestClassScope1(): def test_module_scope_1(self, module_scope): assert module_scope == "测试module_scope" class TestClassScope2(): def test_module_scope_2(self, module_scope): assert module_scope == "测试module_scope" #运行结果 ============================= test session starts ============================= platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- D:\program\Python37\python.exe cachedir: .pytest_cache rootdir: E:\virtual_workshop\pytest-demo\test_demo2 collecting ... collected 2 items test_module_scope.py::TestClassScope1::test_module_scope_1 每个模块开始之前运行一次 PASSED [ 50%] test_module_scope.py::TestClassScope2::test_module_scope_2 PASSED [100%] ============================== 2 passed in 0.03s ==============================

5|0五. scope="session"


在test_demo3包下定义一个conftest.py,代码如下:

import pytest @pytest.fixture(scope="session") def session_scope(): print("一个session(多个模块)开始之前调用一次") msg = "一个session(多个模块)开始之前调用一次" return msg

然后定义两个模块,一个是test_session_scope1.py,一个是test_session_scope2.py

#test_session_scope1.py def test_session_scope1(session_scope): assert session_scope == "测试session_scope" #test_session_scope2.py def test_session_scope2(session_scope): assert session_scope == "测试session_scope" #运行结果 ============================= test session starts ============================= platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- D:\program\Python37\python.exe cachedir: .pytest_cache rootdir: E:\virtual_workshop\pytest-demo\test_demo3 collecting ... collected 2 items test_session_scope1.py::test_session_scope1 一个session(多个模块)开始之前运行一次 PASSED [ 50%] test_session_scope2.py::test_session_scope2 PASSED [100%] ============================== 2 passed in 0.03s ==============================

6|0六. 实例化顺序


session > module > class > function。一个比较好的例子是这篇博客中的:

import pytest order = [] @pytest.fixture(scope="session") def s1(): order.append("s1") @pytest.fixture(scope="module") def m1(): order.append("m1") @pytest.fixture def f1(f3, a1): # 先实例化f3, 再实例化a1, 最后实例化f1 order.append("f1") assert f3 == 123 @pytest.fixture def f3(): order.append("f3") a = 123 yield a @pytest.fixture def a1(): order.append("a1") @pytest.fixture def f2(): order.append("f2") def test_order(f1, m1, f2, s1): # m1、s1在f1后,但因为scope范围大,所以会优先实例化 assert order == ["s1", "m1", "f3", "a1", "f1", "f2"]

 


__EOF__

本文作者cnhkzyy
本文链接https://www.cnblogs.com/my_captain/p/12714119.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   cnhkzyy  阅读(341)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示