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

Pytest中的fixture装饰器详解

pytest是Python生态中最流行的自动化测试框架,它通过简洁的语法、强大的功能(如fixture、参数化、插件扩展等)和丰富的插件生态,帮助开发者高效完成单元测试、集成测试和端到端测试。fixture是pytest框架中最核心、最强大的功能之一,它提供了一种优雅的方式来管理测试依赖项(如数据库连接、测试数据、API客户端等),实现了测试代码的模块化和复用。

fixture基础概念

  1. fixture的作用
  • 依赖注入:自动将资源(如数据库连接)注入到测试函数中
  • 资源管理:统一管理测试资源的创建和清理
  • 复用性:多个测试用例可以共享同一个fixture

2.基本语法


import pytest
@pytest.fixture
def fixture_name():# 初始化代码resource = create_resource()yield resource  # 返回资源给测试用例使用# 清理代码(测试完成后执行)resource.cleanup()

2.fixture核心特性

  1. 作用域控制
  • 通过scope参数控制fixture的生命周期:

  • function(默认):每个测试函数执行一次

  • class:每个测试类执行一次

  • module:每个Python模块执行一次

  • package:每个包执行一次

  • session:整个测试会话执行一次

@pytest.fixture(scope="module")
def shared_resource():return create_expensive_resource()
  1. 自动使用

通过autouse=True让fixture自动执行,无需显式调用


@pytest.fixture
def db_connection():return connect_to_database()
@pytest.fixture
def user_repo(db_connection):return UserRepository(db_connection)
  1. 依赖其他fixture

fixture可以依赖其他fixture


@pytest.fixture
def db_connection():return connect_to_database()
@pytest.fixture
def user_repo(db_connection):return UserRepository(db_connection)

3.fixture使用案例

1)API客户端初始化

import pytest
import requests
@pytest.fixture(scope="module")
def api_client():client = requests.Session()client.base_url = "https://api.example.com"client.headers = {"Authorization": "Bearer test_token"}yield client  # 整个测试模块共享同一个客户端client.close()  # 模块测试完成后关闭
def test_get_user(api_client):response = api_client.get("/users/1")assert response.status_code == 200assert response.json()["name"] == "Alice"

2)Web浏览器实例管理(结合Selenium)

import pytest
from selenium import webdriver
@pytest.fixture(scope="class")
def browser():driver = webdriver.Chrome()driver.implicitly_wait(10)yield driver  # 整个测试类共享同一个浏览器实例driver.quit()  # 类测试完成后关闭浏览器
class TestWebLogin:def test_login(self, browser):browser.get("https://example.com/login")browser.find_element("#username").send_keys("admin")browser.find_element("#password").send_keys("secret")browser.find_element("#submit").click()assert "Dashboard" in browser.title

4.fixture高级技巧

1)动态fixture参数化

@pytest.fixture(params=["sqlite", "mysql", "postgres"])
def database(request):return create_db_connection(request.param)
def test_db_operations(database):assert database.execute("SELECT 1") == 1

2)多个fixture组合

@pytest.fixture
def auth_token():return "test_token_123"
@pytest.fixture
def authenticated_client(api_client, auth_token):api_client.headers["Authorization"] = f"Bearer {auth_token}"return api_client
def test_protected_endpoint(authenticated_client):response = authenticated_client.get("/protected")assert response.status_code == 200

在这里插入图片描述

http://www.xdnf.cn/news/2963.html

相关文章:

  • OpenCV 图形API(72)图像与通道拼接函数-----根据指定的方式翻转图像(GMat)函数 flip()
  • 布局元素组件 (Layout Element)
  • 功放IC搭配的升压芯片选型指南:为何FP5207更适合高保真功放系统?
  • 基于大模型的大肠息肉全程管理研究报告
  • 东土科技NewPre系列智能控制器的创新之旅
  • 第17节:传统分类模型-随机森林与决策树
  • 【Prometheus-Mongodb Exporter安装配置指南,开机自启】
  • 【安全扫描器原理】ICMP扫描
  • Docker基础(安装和命令)
  • 第三节:用户和用户组管理
  • 测试——BUG篇
  • python类中的 __contains__方法是什么?
  • unity Orbbec Femto Bolt接入unity流程记录 AzureKinectExamples 插件 使用记录
  • oracle 批量查询每张表的数据量
  • RoPE 相对位置编码 VS 传统位置编码
  • neo4j vs python
  • Canal使用
  • 巧记英语四级单词 Unit7-上【晓艳老师版】
  • 【应用密码学】实验三 流密码(ZUC)
  • 智能电子白板的设计与实现:从硬件选型到软件编程
  • 【ArcGIS微课1000例】0143:什么是ovkml,如何转换为kml与shp?
  • 使用 OpenCV 实现图像中心旋转
  • SpringBoot获取用户信息常见问题(密码屏蔽、驼峰命名和下划线命名的自动转换)
  • Spring Cloud 项目中优雅地传递用户信息:基于 Gateway + ThreadLocal 的用户上下文方案
  • 破解工业协议孤岛:迈威MaxGate系列工业智能网关覆盖全场景需求
  • docker-vllm运行大模型
  • KUKA机器人快速启动设置
  • python:sklearn 主成分分析(PCA)
  • 本地大模型编程实战(28)查询图数据库NEO4J(1)
  • 四.割草机技术总结--4.基站发送给流动站的差分数据传输标准RTCM