一. 定义 pytest的fixture中有一个参数scope,它的作用域有五个,分别是:function、class、module、和session function:每个方法开始之前都会调用一次,方法级别 class:每个类开始之前都会调用
一. 定义
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 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 ==============================
三. 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 ==============================
四. 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 ==============================
五. 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.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 ==============================
六. 实例化顺序
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.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"]