fix(report): extract Chinese test name from docstring metadata field
Priority: 用例名称: xxx > display_name > docstring first line
This commit is contained in:
+21
-8
@@ -122,23 +122,36 @@ def pytest_sessionfinish(session):
|
||||
|
||||
|
||||
def _resolve_cn_name(session, func_name: str) -> str:
|
||||
"""从 pytest session 中查找函数的中文 display_name。
|
||||
"""从用例元信息中提取中文名称。
|
||||
|
||||
策略:
|
||||
1) 查找测试函数上的 display_name 属性 (test_xxx.display_name = "中文名")
|
||||
2) 使用 item.name (含 nodeid),提取函数名并取 docstring 第一行去掉 【P0】 前缀
|
||||
3) 兜底返回原始函数名
|
||||
优先级:
|
||||
1) 用例 docstring 中显式声明的 "用例名称: xxx"
|
||||
2) display_name 属性
|
||||
3) docstring 第一行去 【P0】 前缀
|
||||
|
||||
用例 docstring 格式示例:
|
||||
用例ID: TC-D2S-001
|
||||
用例名称: 司机APP接单到管理端核算打款完整流程
|
||||
优先级: P0
|
||||
"""
|
||||
for item in session.items:
|
||||
item_func_name = getattr(item, "originalname", None) or item.name.rsplit("::", 1)[-1]
|
||||
if item_func_name == func_name:
|
||||
test_cls = item.cls if hasattr(item, "cls") else None
|
||||
test_func = getattr(test_cls, item_func_name, None) if test_cls else None
|
||||
# 策略1: display_name 属性
|
||||
doc = (test_func.__doc__ or "") if test_func else ""
|
||||
|
||||
# 策略1: 从 docstring 中提取 "用例名称: xxx"
|
||||
import re
|
||||
m = re.search(r"用例名称[::]\s*(.+)", doc)
|
||||
if m:
|
||||
return m.group(1).strip()
|
||||
|
||||
# 策略2: display_name 属性
|
||||
if test_func and hasattr(test_func, "display_name"):
|
||||
return test_func.display_name
|
||||
# 策略2: docstring 第一行
|
||||
doc = (test_func.__doc__ or "") if test_func else ""
|
||||
|
||||
# 策略3: docstring 第一行去 【P0】 前缀
|
||||
if doc:
|
||||
first_line = doc.strip().split("\n")[0].strip()
|
||||
if first_line.startswith("【"):
|
||||
|
||||
Reference in New Issue
Block a user