- CLI + Web 双平台,基于 LLM 的 TDD+DDD 自动化测试生成 - 集成 unified-test-platform 903 个 D-Cloud 用例 + 13 个 Robogo 场景 - Agent 5 步流水线: 扫描 -> DDD -> LLM 生成测试 - 精确回归 + UI 变更检查 - 可视化 HTML 测试报告 - 暗色/亮色主题切换 - 19 个 API 端点 + 6 个 SPA 页面 - 15/15 项 TDD+DDD 合规
142 lines
3.9 KiB
Python
142 lines
3.9 KiB
Python
"""
|
|
测试数据工厂
|
|
|
|
提供统一的测试数据生成和清理机制:
|
|
- 自动生成唯一名称(使用 UUID/时间戳)
|
|
- 支持数据生命周期管理
|
|
- 与 pytest fixture 集成
|
|
"""
|
|
import uuid
|
|
import time
|
|
from typing import Optional, List, Callable
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass
|
|
class TestData:
|
|
"""测试数据容器"""
|
|
name: str
|
|
created_at: float
|
|
cleanup: Optional[Callable] = None
|
|
|
|
|
|
class DataFactory:
|
|
"""测试数据工厂
|
|
|
|
示例用法:
|
|
# 在测试中
|
|
task_name = DataFactory.task_name()
|
|
space_name = DataFactory.space_name("团队空间")
|
|
|
|
# 使用 fixture
|
|
@pytest.fixture
|
|
def test_task(api_client):
|
|
name = DataFactory.task_name()
|
|
# 创建任务...
|
|
yield name
|
|
# 清理
|
|
api_client.delete(f"/api/tasks/{name}")
|
|
"""
|
|
|
|
# 前缀映射
|
|
_prefixes = {
|
|
"task": "测试任务",
|
|
"space": "测试空间",
|
|
"dataset": "测试数据集",
|
|
"model": "测试模型",
|
|
"project": "测试项目",
|
|
"file": "测试文件",
|
|
}
|
|
|
|
@classmethod
|
|
def _generate_name(cls, category: str, suffix: str = None) -> str:
|
|
"""生成唯一名称"""
|
|
prefix = cls._prefixes.get(category, "测试")
|
|
unique_id = uuid.uuid4().hex[:8]
|
|
timestamp = int(time.time())
|
|
parts = [prefix, unique_id]
|
|
if suffix:
|
|
parts.append(suffix)
|
|
return "_".join(parts)
|
|
|
|
@classmethod
|
|
def task_name(cls, suffix: str = None) -> str:
|
|
"""生成任务名称"""
|
|
return cls._generate_name("task", suffix)
|
|
|
|
@classmethod
|
|
def space_name(cls, suffix: str = None) -> str:
|
|
"""生成空间名称"""
|
|
return cls._generate_name("space", suffix)
|
|
|
|
@classmethod
|
|
def dataset_name(cls, suffix: str = None) -> str:
|
|
"""生成数据集名称"""
|
|
return cls._generate_name("dataset", suffix)
|
|
|
|
@classmethod
|
|
def model_name(cls, suffix: str = None) -> str:
|
|
"""生成模型名称"""
|
|
return cls._generate_name("model", suffix)
|
|
|
|
@classmethod
|
|
def project_name(cls, suffix: str = None) -> str:
|
|
"""生成项目名称"""
|
|
return cls._generate_name("project", suffix)
|
|
|
|
@classmethod
|
|
def file_name(cls, extension: str = "txt", suffix: str = None) -> str:
|
|
"""生成文件名"""
|
|
name = cls._generate_name("file", suffix)
|
|
return f"{name}.{extension}"
|
|
|
|
@classmethod
|
|
def unique_id(cls) -> str:
|
|
"""生成唯一ID"""
|
|
return uuid.uuid4().hex[:8]
|
|
|
|
@classmethod
|
|
def timestamp(cls) -> int:
|
|
"""获取当前时间戳"""
|
|
return int(time.time())
|
|
|
|
|
|
class DataRegistry:
|
|
"""测试数据注册表
|
|
|
|
用于跟踪和批量清理测试数据:
|
|
registry = DataRegistry()
|
|
|
|
# 注册数据
|
|
registry.register("task", "测试任务_abc123", cleanup=lambda: api.delete("/tasks/abc123"))
|
|
|
|
# 批量清理
|
|
registry.cleanup_all()
|
|
"""
|
|
|
|
def __init__(self):
|
|
self._data: List[TestData] = []
|
|
|
|
def register(self, name: str, cleanup: Callable = None) -> TestData:
|
|
"""注册测试数据"""
|
|
data = TestData(name=name, created_at=time.time(), cleanup=cleanup)
|
|
self._data.append(data)
|
|
return data
|
|
|
|
def cleanup_all(self):
|
|
"""清理所有注册的数据"""
|
|
for data in reversed(self._data): # 按创建顺序的逆序清理
|
|
if data.cleanup:
|
|
try:
|
|
data.cleanup()
|
|
except Exception as e:
|
|
print(f"[清理失败] {data.name}: {e}")
|
|
self._data.clear()
|
|
|
|
def get_names(self) -> List[str]:
|
|
"""获取所有注册的数据名称"""
|
|
return [d.name for d in self._data]
|
|
|
|
|
|
# 全局注册表实例
|
|
registry = DataRegistry() |