1 Star 1 Fork 2

dingyiaaa/ApiTestFramework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
conftest.py 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
DD 提交于 2025-04-29 17:52 +08:00 . 完善框架: excel解析优化
# -*- coding: utf-8 -*-
import pytest
from common.excel_handler import load_test_cases
from common.logger import get_logger
from common.request_handler import handle_precondition
logger = get_logger()
# Global data storage, used to hold session-level variables
pytest.global_data = {}
class NonUnicodeStr(str):
def __repr__(self):
return self
def __str__(self):
return self
# Fixture to load test cases from the Excel file
@pytest.fixture(scope="session")
def test_cases():
# 从 Excel 文件中读取所有测试用例数据
return load_test_cases()
# Fixture for setting up and tearing down preconditions or environment setup
@pytest.fixture(scope="session", autouse=True)
def setup_environment():
# 这里可以做一些全局的前置操作,如数据库连接、初始化等
print("环境准备中...")
handle_precondition()
yield
# 测试结束后做清理工作
print("环境清理中...")
# pytest_generate_tests 用于动态生成测试数据
def load_and_filter_test_cases():
# 假设 load_test_cases() 从 Excel 中加载测试数据
cases = load_test_cases()[1]
# 过滤掉没有 '模块' 或 '功能点' 键的字典
cases = [c for c in cases if 'module' in c and 'name' in c]
return cases
def pytest_generate_tests(metafunc):
if "case" in metafunc.fixturenames:
# 加载用例数据
try:
cases = load_and_filter_test_cases() # 你自己的封装函数
except Exception as e:
raise RuntimeError(f"加载测试用例失败: {e}")
if not cases:
raise ValueError("❌ 用例数据为空,无法执行测试!")
# 检查字段并构建 id
ids = []
for i, case in enumerate(cases):
num = str(case.get("num") or f"用例{case.get('case_id')}").strip()
module = str(case.get("module") or f"模块{case.get('case_id')}").strip()
name = str(case.get("name") or f"用例{case.get('case_id')}").strip()
logger.info(f"========={num} {module}-{name}")
if not module or not name:
print(f"---第 {i} 条用例缺字段:{case}")
ids.append(NonUnicodeStr(f"{module}-{name}"))
# parametrize 生效
metafunc.parametrize("case", cases, ids=ids)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dingyiaaa/ApiTestFramework.git
git@gitee.com:dingyiaaa/ApiTestFramework.git
dingyiaaa
ApiTestFramework
ApiTestFramework
master

搜索帮助