feat(executor): 重构用例加载与执行逻辑,支持参数化变量优先级

- 引入 CaseEntity 包装器,实现数据模型与执行上下文解耦。
 - 移除加载阶段的 deepcopy,优化大规模参数化用例的内存占用。
 - 实现 perform 阶段的局部变量注入,确保参数化数据优先级高于全局缓存。
This commit is contained in:
2026-03-11 17:11:19 +08:00
parent 293b5160fe
commit 2116016a0d
7 changed files with 201 additions and 69 deletions

View File

@@ -7,7 +7,7 @@
import logging
import importlib
from typing import Any, List
from typing import Any, List, Optional
from pydantic import TypeAdapter
@@ -28,9 +28,20 @@ class WorkflowExecutor:
self.session = session
self.exchanger = exchanger
def perform(self, case_info: CaseInfo) -> Any:
def perform(self, case_info: CaseInfo,context: Optional[dict[str, Any]] = None) -> Any:
"""执行单个用例支持直接请求和PO模式调用"""
context = context or {}
# 1. 局部变量优先级注入
# 备份全局缓存,将当前行数据合并进去
old_cache = self.exchanger._variable_cache.copy()
self.exchanger._variable_cache.update(context)
try:
# 2. 动态更新标题(如果 context 中包含 title
current_title = context.get("title") or case_info.title
logger.info(f"🚀 执行用例: {current_title}")
# raw_data = case_info.model_dump(by_alias=True, exclude_none=True)
# 1. 变量替换(将 ${var} 替换为真实值)
# rendered_dict = self.exchanger.replace(raw_data)
@@ -62,6 +73,10 @@ class WorkflowExecutor:
except Exception as e:
logger.error(f"用例执行失败: {case_info.title} | 原因: {e}", exc_info=True)
raise
finally:
# 4. 关键:清理现场,还原全局变量池
self.exchanger._variable_cache = old_cache
def _execute_po_method(self, action: ApiActionModel):
"""核心反射逻辑:根据字符串动态加载 api/ 目录下的类并执行方法"""