当前位置: 首页 > article >正文

Pytest Fixture 详解

Pytest Fixture 详解

Fixture 是 pytest 最强大的功能之一,用于提供测试所需的依赖资源(如数据库连接、临时文件、模拟对象等),并支持复用、作用域控制和自动清理。以下是全面详解:


1. 基本用法

定义 Fixture

使用 @pytest.fixture 装饰器定义:

import pytest@pytest.fixture
def database_connection():conn = create_db_connection()  # 初始化资源yield conn                     # 返回资源,测试结束后执行清理conn.close()                   # 清理操作(yield 之后的部分)

使用 Fixture

在测试函数中作为参数传入:

def test_query_data(database_connection):result = database_connection.execute("SELECT * FROM users")assert len(result) > 0

2. Fixture 作用域(Scope)

通过 scope 参数控制 Fixture 的生命周期:

作用域说明使用场景
function每个测试函数运行一次(默认)轻量级资源(如临时变量)
class每个测试类运行一次类共享资源(如配置对象)
module每个测试模块(文件)运行一次全局配置(如日志初始化)
package每个测试包运行一次包级共享资源
session整个测试会话只运行一次昂贵资源(如数据库连接池)

示例

@pytest.fixture(scope="module")
def shared_config():return {"timeout": 30}

3. Fixture 的 Setup/Teardown

使用 yieldaddfinalizer 实现资源清理:

yield 方式(推荐)

@pytest.fixture
def temp_file():file = open("/tmp/test.txt", "w")yield file  # 测试中使用 file 对象file.close()  # 测试结束后关闭文件

addfinalizer 方式(更灵活)

@pytest.fixture
def temp_file(request):file = open("/tmp/test.txt", "w")def cleanup():file.close()request.addfinalizer(cleanup)  # 注册清理函数return file

4. Fixture 参数化

@pytest.mark.parametrize 对 Fixture 参数化:

@pytest.fixture(params=["user1", "user2", "admin"])
def user(request):return create_user(request.param)  # 根据参数生成不同用户def test_user_permissions(user):assert user.has_permission("read")

5. 自动使用 Fixture(autouse)

设置 autouse=True,无需显式调用即可自动运行:

@pytest.fixture(autouse=True)
def setup_logging():logging.basicConfig(level=logging.INFO)  # 所有测试自动启用日志def test_example():assert True  # 此测试会自动执行 setup_logging

6. Fixture 依赖注入

Fixture 可以依赖其他 Fixture:

@pytest.fixture
def db():return Database()@pytest.fixture
def api_client(db):  # 依赖 db fixturereturn APIClient(db)def test_api(api_client):response = api_client.get("/users")assert response.status_code == 200

7. 动态 Fixture

通过 request 对象动态调整 Fixture:

@pytest.fixture
def dynamic_data(request):marker = request.node.get_closest_marker("data")if marker:return marker.args[0]  # 获取测试标记的参数return {"default": 42}@pytest.mark.data({"custom": 100})
def test_dynamic(dynamic_data):assert dynamic_data["custom"] == 100

8. 内置 Fixture

pytest 提供了一些常用内置 Fixture:

Fixture说明
tmp_path临时目录路径(pathlib.Path
tmpdir临时目录(Legacy py.path.local
capsys捕获 stdout/stderr
monkeypatch动态修改对象或环境变量
request访问测试上下文(如参数、标记)

示例

def test_tmp_file(tmp_path):file = tmp_path / "test.txt"file.write_text("Hello")assert file.read_text() == "Hello"

9. Fixture 覆盖与插件

覆盖 Fixture

conftest.py 中定义的 Fixture 可被当前目录及子目录的测试共享:

project/
├── conftest.py         # 定义全局 Fixture
├── tests/
│   ├── conftest.py     # 覆盖父级 Fixture
│   └── test_demo.py

Fixture 插件

通过插件扩展 Fixture(如 pytest-django 提供 django_db):

def test_django_model(django_db):user = User.objects.create(name="Alice")assert user.name == "Alice"

10. 最佳实践

  1. 将 Fixture 定义在 conftest.py 中以便复用。
  2. 避免 Fixture 逻辑过于复杂,保持单一职责。
  3. 优先使用 yield 而非 addfinalizer(代码更简洁)。
  4. scope 减少重复初始化(如数据库连接池用 session 作用域)。

通过灵活使用 Fixture,你可以实现:

  • 资源复用(减少重复代码)
  • 依赖管理(清晰表达测试需求)
  • 环境隔离(避免测试间干扰)

掌握后,测试代码会变得更简洁、可维护! 🚀

http://www.lryc.cn/news/2400671.html

相关文章:

  • 力扣HOT100之二分查找:74. 搜索二维矩阵
  • 【前端】前后端通信
  • 编程技能:格式化打印04,sprintf
  • C语言基础(11)【函数1】
  • R语言基础| 下载、安装
  • 【hive sql】窗口函数
  • Ubuntu24.04 交叉编译 aarch64 ffmpeg
  • 《AI角色扮演反诈技术解析:原理、架构与核心挑战》
  • 微软的新系统Windows12未来有哪些新特性
  • 树莓派超全系列教程文档--(54)如何使用rsync在计算机之间同步文件夹
  • 华为ICT和AI智能应用
  • ROS2与Unitree机器人集成指南
  • 在虚拟宇宙中低语——进程间通信,Linux命名管道的前世今生
  • Cursor 工具项目构建指南:Java 21 环境下的 Spring Boot Prompt Rules 约束
  • 各个布局的区别以及示例
  • 什么是MVC?
  • STM32的ADC简介
  • Bash shell四则运算
  • (javaSE)Java数组进阶:数组初始化 数组访问 数组中的jvm 空指针异常
  • 力扣刷题Day 70:在排序数组中查找元素的第一个和最后一个位置(34)
  • vue 多端适配之pxtorem
  • 图片压缩工具 | 图片属性详解及读取解析元数据
  • React---day8
  • C# Onnx 动漫人物人脸检测
  • C++内存列传之RAII宇宙:智能指针
  • PVE 虚拟机安装 Ubuntu Server V24 系统 —— 一步一步安装配置基于 Ubuntu Server 的 NodeJS 服务器详细实录1
  • GitHub 趋势日报 (2025年06月03日)
  • 出现dev/nvmeOnip2 contains a file system with errors, check forced 解决方法
  • Vue3.5 企业级管理系统实战(二十二):动态菜单
  • 磨皮功能 C++/C的OpenCV 实现