212 lines
6.8 KiB
Markdown
212 lines
6.8 KiB
Markdown
# 测试阶段 Prompt 模板
|
|
|
|
## 目标
|
|
|
|
运行项目现有测试 + 开发生成的新测试,确保代码质量。
|
|
|
|
## 测试流程
|
|
|
|
```
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
│ 测试阶段流程 │
|
|
├─────────────────────────────────────────────────────────────┤
|
|
│ │
|
|
│ 1. 检测测试框架 │
|
|
│ └→ pytest / jest / go test / ... │
|
|
│ ↓ │
|
|
│ 2. 运行现有测试 │
|
|
│ └→ 记录结果 │
|
|
│ ↓ │
|
|
│ 3. 运行新增测试 │
|
|
│ └→ 记录结果 │
|
|
│ ↓ │
|
|
│ ┌───────────────────────────────────────┐ │
|
|
│ │ 测试结果 │ │
|
|
│ ├───────────────────────────────────────┤ │
|
|
│ │ 全部通过 → 进入下一阶段 │ │
|
|
│ │ 有失败 → 重试(最多3次) │ │
|
|
│ │ 超过重试 → 汇报用户 │ │
|
|
│ └───────────────────────────────────────┘ │
|
|
│ │
|
|
└─────────────────────────────────────────────────────────────┘
|
|
```
|
|
|
|
## 测试框架检测
|
|
|
|
```python
|
|
def detect_test_framework(project_root: str) -> dict:
|
|
"""检测项目的测试框架"""
|
|
|
|
frameworks = []
|
|
|
|
# Python
|
|
if (Path(project_root) / "pytest.ini").exists():
|
|
frameworks.append({"name": "pytest", "command": "pytest -v", "type": "python"})
|
|
if (Path(project_root) / "setup.py").exists():
|
|
frameworks.append({"name": "unittest", "command": "python -m unittest", "type": "python"})
|
|
|
|
# JavaScript/TypeScript
|
|
if (Path(project_root) / "jest.config.js").exists():
|
|
frameworks.append({"name": "jest", "command": "npm test", "type": "javascript"})
|
|
if (Path(project_root) / "vitest.config.ts").exists():
|
|
frameworks.append({"name": "vitest", "command": "npx vitest run", "type": "javascript"})
|
|
|
|
# Go
|
|
if (Path(project_root) / "go.mod").exists():
|
|
frameworks.append({"name": "go test", "command": "go test ./...", "type": "go"})
|
|
|
|
return frameworks
|
|
```
|
|
|
|
## 测试执行 Prompt
|
|
|
|
```
|
|
你是一个测试工程师。请执行以下测试任务。
|
|
|
|
## 项目信息
|
|
- 项目路径: {project_root}
|
|
- 测试框架: {test_framework}
|
|
|
|
## 任务
|
|
|
|
1. 运行所有现有测试
|
|
2. 运行本次开发新增的测试
|
|
3. 记录测试结果
|
|
4. 分析失败原因(如果有)
|
|
|
|
## 输出格式
|
|
|
|
```markdown
|
|
# 测试报告
|
|
|
|
## 测试环境
|
|
- 框架: {framework}
|
|
- 命令: {command}
|
|
- 执行时间: {timestamp}
|
|
|
|
## 测试结果摘要
|
|
|
|
| 类型 | 总数 | 通过 | 失败 | 跳过 |
|
|
|------|------|------|------|------|
|
|
| 现有测试 | {total} | {passed} | {failed} | {skipped} |
|
|
| 新增测试 | {total} | {passed} | {failed} | {skipped} |
|
|
|
|
## 失败详情
|
|
|
|
### 失败测试 1: {test_name}
|
|
```
|
|
{error_message}
|
|
```
|
|
|
|
**可能原因**: {analysis}
|
|
|
|
## 结论
|
|
- [ ] 全部通过
|
|
- [ ] 有失败需要修复
|
|
|
|
## 建议
|
|
{recommendations}
|
|
```
|
|
|
|
请执行测试并生成报告。
|
|
```
|
|
|
|
## 测试结果解析
|
|
|
|
```python
|
|
def parse_test_results(output: str, framework: str) -> dict:
|
|
"""解析测试结果"""
|
|
|
|
if framework == "pytest":
|
|
# 解析 pytest 输出
|
|
import re
|
|
match = re.search(r"(\d+) passed, (\d+) failed", output)
|
|
if match:
|
|
return {
|
|
"passed": int(match.group(1)),
|
|
"failed": int(match.group(2))
|
|
}
|
|
|
|
elif framework == "jest":
|
|
# 解析 jest 输出
|
|
import re
|
|
match = re.search(r"(\d+) passed, (\d+) failed", output)
|
|
if match:
|
|
return {
|
|
"passed": int(match.group(1)),
|
|
"failed": int(match.group(2))
|
|
}
|
|
|
|
return {"passed": 0, "failed": 0, "raw_output": output}
|
|
```
|
|
|
|
## 重试逻辑
|
|
|
|
```python
|
|
def run_tests_with_retry(max_retries: int = 3) -> dict:
|
|
"""带重试的测试执行"""
|
|
|
|
for attempt in range(1, max_retries + 1):
|
|
print(f"\n尝试 {attempt}/{max_retries}...")
|
|
|
|
result = run_tests()
|
|
|
|
if result["failed"] == 0:
|
|
print("✅ 测试全部通过")
|
|
return result
|
|
|
|
print(f"⚠️ 有 {result['failed']} 个测试失败")
|
|
|
|
if attempt < max_retries:
|
|
print("准备重试...")
|
|
# 可以在这里添加自动修复逻辑
|
|
|
|
print(f"❌ 已达到最大重试次数 ({max_retries})")
|
|
return result
|
|
```
|
|
|
|
## 测试报告保存
|
|
|
|
```json
|
|
{
|
|
"phases": {
|
|
"7": {
|
|
"status": "completed",
|
|
"test_results": {
|
|
"framework": "pytest",
|
|
"existing_tests": {"total": 20, "passed": 20, "failed": 0},
|
|
"new_tests": {"total": 5, "passed": 5, "failed": 0},
|
|
"all_passed": true,
|
|
"retry_count": 0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## 失败处理
|
|
|
|
如果测试失败且超过重试次数:
|
|
|
|
```
|
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
⚠️ 测试失败
|
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
|
|
已重试 3 次,仍有 {failed_count} 个测试失败:
|
|
|
|
## 失败的测试
|
|
1. {test_1}
|
|
2. {test_2}
|
|
|
|
## 可能的原因
|
|
{analysis}
|
|
|
|
## 请选择:
|
|
A. 自动修复 - 安排子代理修复问题
|
|
B. 手动处理 - 我来查看和修复
|
|
C. 跳过测试 - 继续下一阶段(不推荐)
|
|
|
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
```
|