文章目录
- 前言
- Scope 作用域
- 写在测试用例函数文件
- 写在`conftest.py`文件
- 作用域总结
- 验证默认作用域
- 验证执行顺序遵循
- 验证类中的fixture作用域
- 验证重名fixture作用域

前言
从前文中,我们已经知道
固件(fixture)的概念、原理、作用域,并且举出很多自定义的前后夹具;
本文不再赘述这些,将继续深入讲解
固件(fixture)的作用域(scope)参数。
Scope 作用域
固件(fixture) 的作用域决定了其被调用的频率和生命周期;
主要分为两部分:
写在测试用例函数文件中
写在conftest.py文件中:
conftest.py是pytest特有且固定的py模块
写在测试用例函数文件
表示被
@pytest.fixture标记的函数的作用域
| 属性 | 描述 |
|---|---|
function | 默认值, 对于每个测试用例(函数或方法),fixture 会在其执行前被调用一次 |
class | 对于每个测试类,fixture 会在所有测试用例执行前被调用一次 |
module | 对于每个Python模块,fixture 会在该模块中的所有测试用例执行前被调用一次 |
package | 对于整个Python包,fixture会在该包中的所有测试用例执行前被调用一次 |
session | 对于整个测试,会话中的所有模块、包和测试函数中只调用一次 |
写在conftest.py文件
如果
fixture写在conftest.py文件中,作用则略有不同;主要体现在测试用例的可见性和重用性。
| 属性 | 描述 |
|---|---|
function | 每一个测试文件中的所有测试用例执行前都会执行一次conftest文件中的fixture |
class | 每一个测试文件中的测试类执行前都会执行一次conftest文件中的fixture |
module | 每一个测试文件执行前都会执行一次conftest文件中的fixture |
package | 当前目录下所有测试py文件执行前执行一次conftest文件中的fixture |
session | 所有测试py文件执行前执行一次conftest文件中的fixture |
作用域总结
- scope参数的默认值:
function- 作用域执行顺序遵循:
session>package>module>class>function- 模块和类中的fixture优先级:当模块和类中有同名的
fixture时,类的fixture会优先被使用。这是因为类的fixture是局部的,而模块的fixture是全局的。
验证默认作用域
import pytest
@pytest.fixture
def setup_teardown_per_test():
print("\n前置")
yield
print("\n后置")
def test_example1(setup_teardown_per_test):
assert 1 == 1
def test_example2(setup_teardown_per_test):
assert 2 == 2
class TestCase01:
def test_02_a(self):
print("正在执行 test_02_a 函数...")
def test_02_b(self):
print("正在执行 test_02_b 函数...")

验证执行顺序遵循
import pytest
@pytest.fixture(autouse=True, scope="function")
def fun():
print("---fixture : function-前")
yield
print("---fixture : function-后")
@pytest.fixture(autouse=True, scope="class")
def fun2():
print("---fixture : class-前")
yield
print("---fixture : class-后")
@pytest.fixture(autouse=True, scope="module")
def fun3():
print("---fixture : module-前")
yield
print("---fixture : module-后")
@pytest.fixture(autouse=True, scope="package")
def fun4():
print("---fixture : package-前")
yield
print("---fixture : package-后")
@pytest.fixture(autouse=True, scope="session")
def fun5():
print("---fixture : session-前")
yield
print("---fixture : session-后")
def test_a():
print("--------------test_a")
def test_b():
print("--------------test_b")
class Test01Scope:
def test_c(self):
print("--------------test_c")
def test_d(self):
print("--------------test_d")

验证类中的fixture作用域
import pytest
def test_a():
print("--------------test_a")
def test_b():
print("--------------test_b")
class Test01Scope:
def test_c(self):
print("--------------test_c")
def test_d(self):
print("--------------test_d")
@pytest.fixture(autouse=True, scope="function")
def fun(self):
print("---fixture : function-前")
yield
print("---fixture : function-后")
@pytest.fixture(autouse=True, scope="class")
def fun2(self):
print("---fixture : class-前")
yield
print("---fixture : class-后")
@pytest.fixture(autouse=True, scope="module")
def fun3(self):
print("---fixture : module-前")
yield
print("---fixture : module-后")
@pytest.fixture(autouse=True, scope="package")
def fun4(self):
print("---fixture : package-前")
yield
print("---fixture : package-后")
@pytest.fixture(autouse=True, scope="session")
def fun5(self):
print("---fixture : session-前")
yield
print("---fixture : session-后")

验证重名fixture作用域
import pytest
def test_a():
print("--------------test_a")
def test_b():
print("--------------test_b")
@pytest.fixture(autouse=True, scope="function")
def fun():
print("---fixture : function-前")
yield
print("---fixture : function-后")
@pytest.fixture(autouse=True, scope="class")
def fun2():
print("---fixture : class-前")
yield
print("---fixture : class-后")
@pytest.fixture(autouse=True, scope="module")
def fun3():
print("---fixture : module-前")
yield
print("---fixture : module-后")
@pytest.fixture(autouse=True, scope="package")
def fun4():
print("---fixture : package-前")
yield
print("---fixture : package-后")
@pytest.fixture(autouse=True, scope="session")
def fun5():
print("---fixture : session-前")
yield
print("---fixture : session-后")
class Test01Scope:
def test_c(self):
print("--------------test_c")
def test_d(self):
print("--------------test_d")
@pytest.fixture(autouse=True, scope="function")
def fun(self):
print("---fixture : function-前(类中)")
yield
print("---fixture : function-后(类中)")
@pytest.fixture(autouse=True, scope="class")
def fun2(self):
print("---fixture : class-前(类中)")
yield
print("---fixture : class-后(类中)")
@pytest.fixture(autouse=True, scope="module")
def fun3(self):
print("---fixture : module-前(类中)")
yield
print("---fixture : module-后(类中)")
@pytest.fixture(autouse=True, scope="package")
def fun4(self):
print("---fixture : package-前(类中)")
yield
print("---fixture : package-后(类中)")
@pytest.fixture(autouse=True, scope="session")
def fun5(self):
print("---fixture : session-前(类中)")
yield
print("---fixture : session-后(类中)")

















![[论文笔记] Pai-megatron Qwen1.5-14B-CT 后预训练 踩坑记录](https://img-blog.csdnimg.cn/direct/13ad315ebbbd4986a3331d2fd36fc4fd.png)

