一文带你掌握Python pytest的前后置处理机制与方法
作者:Lightning-py
在 pytest 中,确保后置清理代码一定会执行(无论测试通过、失败还是发生异常),推荐使用以下两种最稳健的方式:
1. 使用yield的 fixture(推荐)
在 fixture 的 yield 之后编写清理代码,pytest 会在测试结束后自动运行这些代码,即使测试断言失败或抛出异常。
import pytest
@pytest.fixture
def db_connection():
# 前置:建立连接
conn = create_connection()
yield conn
# 后置:一定会执行(除非 fixture 前置阶段就挂了)
conn.close()
def test_query(db_connection):
assert db_connection.query("SELECT 1") == 1注意:如果 yield 之前的代码(建立连接)抛出了异常,那么 yield 之后的清理代码不会执行,因为 fixture 本身没有成功初始化。这是合理的——资源从未被成功获取,自然无需释放。
2. 使用request.addfinalizer
功能与 yield 类似,但以注册回调函数的方式显式声明清理逻辑。
import pytest
@pytest.fixture
def db_connection(request):
conn = create_connection()
def cleanup():
conn.close()
request.addfinalizer(cleanup) # 注册后置函数
return conn同样,addfinalizer 注册成功后的清理函数一定会执行。
3. 在测试类或模块中使用传统teardown方法
适用于经典的 xUnit 风格,pytest 完全支持:
方法级:def teardown_method(self, method):
类级:def teardown_class(cls):
模块级:def teardown_module(module):
class TestDatabase:
def setup_method(self):
self.conn = create_connection()
def teardown_method(self):
self.conn.close() # 每个测试方法结束后一定会执行
def test_query(self):
assert self.conn.query("SELECT 1") == 14. 如果需要“无论如何(即使 fixture 设置失败)都执行清理”
如果存在必须执行的全局资源回收(例如临时文件、进程等),可以在 fixture 中使用 try/finally:
@pytest.fixture
def temp_file():
f = None
try:
f = open("/tmp/test.txt", "w")
yield f
finally:
if f:
f.close()
# 或者无条件删除文件但这种情况较少见,因为通常设置失败时资源尚未分配,无需清理。
| 方式 | 是否保证后置执行 | 备注 |
|---|---|---|
yield fixture | ✅(前置成功时) | 最推荐,代码简洁 |
request.addfinalizer | ✅(前置成功时) | 与 yield 等价 |
teardown_* 方法 | ✅(对应作用域) | 适用于传统风格 |
try/finally | ✅(无条件) | 适合必须清理的场景 |
5.方法补充
1.使用 setup_function / teardown_function(函数级别)
适用于独立函数的测试用例。
def setup_function(function):
print("\n[前置] 在每个测试函数前执行")
def teardown_function(function):
print("[后置] 在每个测试函数后执行")
def test_add():
assert 1 + 1 == 2
def test_multiply():
assert 2 * 3 == 62.使用 setup_method / teardown_method(类内方法级别)
适用于测试类中的每个测试方法。
class TestMath:
def setup_method(self):
print("\n[前置] 每个测试方法前执行")
def teardown_method(self):
print("[后置] 每个测试方法后执行")
def test_add(self):
assert 1 + 1 == 2
def test_multiply(self):
assert 2 * 3 == 63. 使用 setup_class / teardown_class(类级别)
整个测试类只执行一次前置和后置。
class TestDatabase:
@classmethod
def setup_class(cls):
print("\n[前置] 整个测试类只执行一次")
@classmethod
def teardown_class(cls):
print("[后置] 整个测试类只执行一次")
def test_connection(self):
assert True
def test_query(self):
assert True4. 使用 setup_module / teardown_module(模块级别)
在整个模块(文件)的所有测试前后执行一次。
# test_example.py
def setup_module(module):
print("\n[前置] 模块级别,执行一次")
def teardown_module(module):
print("[后置] 模块级别,执行一次")
def test_a():
assert True
def test_b():
assert True5. 使用 @pytest.fixture(最推荐、最强大)
fixture 是 pytest 的核心功能,可以实现任意粒度的前后置,并支持依赖注入、作用域控制、自动清理等。
基本用法(函数级)
import pytest
@pytest.fixture
def db_connection():
print("\n[前置] 建立数据库连接")
conn = {"host": "localhost", "port": 3306}
yield conn # 将资源传递给测试函数
print("[后置] 关闭数据库连接")
def test_query(db_connection):
assert db_connection["port"] == 3306作用域控制
通过 scope 参数控制生命周期:
| scope | 作用域 |
|---|---|
function | 每个测试函数(默认) |
class | 每个测试类 |
module | 每个模块文件 |
package | 每个包 |
session | 整个测试会话 |
@pytest.fixture(scope="module")
def shared_resource():
print("\n[前置] 模块级别共享资源")
yield {"data": "shared"}
print("[后置] 清理共享资源")自动使用(autouse=True)
无需在测试参数中显式引用,自动在每个作用域执行。
@pytest.fixture(autouse=True)
def auto_log():
print("\n[自动前置] 记录测试开始")
yield
print("[自动后置] 记录测试结束")
def test_something():
assert True # 前后置会自动执行使用 request 对象获取上下文
@pytest.fixture
def resource(request):
print(f"测试函数名: {request.node.name}")
# 可访问 module, cls, function 等
yield
print("清理")6.使用 pytestmark 模块级标记
可以在模块顶部定义 pytestmark 来应用 fixture 到整个模块。
import pytest
pytestmark = pytest.mark.usefixtures("db_connection")
def test_1():
pass
def test_2():
pass7.通过 conftest.py 共享 fixture
将 fixture 定义在 conftest.py 文件中,可被多个测试文件共享。
# conftest.py
import pytest
@pytest.fixture(scope="session")
def global_config():
return {"env": "test"}# test_foo.py
def test_config(global_config):
assert global_config["env"] == "test"8.前后置执行顺序总结
在一个测试函数中,各钩子的执行顺序:
setup_module
setup_class
setup_method
setup_function (对函数测试有效)
<测试函数体>
teardown_function
teardown_method
teardown_class
teardown_module
而 fixture 的 yield 前后代码分别对应前置和后置,且支持嵌套依赖。
推荐实践
- 简单场景:
setup_method/teardown_method或setup_function。 - 复杂/可复用场景:始终优先使用
@pytest.fixture,因为它更灵活、支持依赖注入、作用域可控、更符合 pytest 的设计哲学。 - 全局共享:使用
conftest.py+scope="session"。
通过合理使用这些机制,可以编写出清晰、高效、易于维护的测试用例。
到此这篇关于一文带你掌握Python pytest的前后置处理机制与方法的文章就介绍到这了,更多相关pytest前后置处理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
