pytest中fixture的scope(pytest fixture yield)

网友投稿 452 2022-08-26


pytest中fixture的scope(pytest fixture yield)

一. 定义

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

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

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

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

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

二. scope="function"

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

import pytest@pytest.fixture(scope="function")def function_scope(): print("每个方法开始之前运行一次") msg = "测试function_scope" return msgdef 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.execachedir: .pytest_cacherootdir: E:\virtual_workshop\pytest-demo\test_democollecting ... collected 2 itemstest_function_scope.py::test_function_scope_1 每个方法开始之前运行一次PASSED [ 50%]test_function_scope.py::test_function_scope_2 每个方法开始之前运行一次PASSED [100%]============================== 2 passed in 0.03s ==============================

三. scope="class"

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

import pytest@pytest.fixture(scope="class")def class_scope(): print("每个类开始之前运行一次") msg = "测试class_scope" return msgclass 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.execachedir: .pytest_cacherootdir: E:\virtual_workshop\pytest-demo\test_democollecting ... collected 2 itemstest_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 ==============================

四. scope="module"

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

import pytest@pytest.fixture(scope="module")def module_scope(): print("每个模块开始之前运行一次") msg = "测试module_scope" return msgclass 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.execachedir: .pytest_cacherootdir: E:\virtual_workshop\pytest-demo\test_demo2collecting ... collected 2 itemstest_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 ==============================

五. 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.pydef test_session_scope1(session_scope): assert session_scope == "测试session_scope"#test_session_scope2.pydef 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.execachedir: .pytest_cacherootdir: E:\virtual_workshop\pytest-demo\test_demo3collecting ... collected 2 itemstest_session_scope1.py::test_session_scope1 一个session(多个模块)开始之前运行一次PASSED [ 50%]test_session_scope2.py::test_session_scope2 PASSED [100%]============================== 2 passed in 0.03s ==============================

六. 实例化顺序

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

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:pytest失败重试插件pytest-rerunfailures(pytest报错)
下一篇:对pytest.mark.usefixtures的理解(pytest-allure)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~