教程概述
系列目录:CLI-Anything 教程系列索引
Plan Tests 是 CLI-Anything 7 阶段流程的第四阶段。这一阶段的核心任务是制定完整的测试计划,确保生成的 CLI 质量可靠。
为什么需要测试计划?
flowchart LR
A[无计划测试] --> B[遗漏关键场景]
A --> C[覆盖率不足]
A --> D[测试不可靠]
E[有计划测试] --> F[全面覆盖]
E --> G[80%+ 覆盖率]
E --> H[可重复验证]
style A fill:#ffcccc
style B fill:#ffcccc
style C fill:#ffcccc
style D fill:#ffcccc
style E fill:#e8f5e9
style F fill:#e8f5e9
style G fill:#e8f5e9
style H fill:#e8f5e9
TEST.md 文档结构
# 测试计划 - <软件名称> CLI
## 1. 测试策略概述
- **目标**: 确保 CLI 功能正确、稳定可靠
- **测试类型**: 单元测试 + E2E 测试
- **覆盖率目标**: 单元测试 ≥80%,E2E 测试覆盖核心流程
- **测试框架**: pytest
## 2. 单元测试计划
### 2.1 核心模块测试
| 模块 | 测试点 | 优先级 |
|-----|--------|-------|
| state.py | 状态保存/加载 | P0 |
| state.py | 撤销/重做 | P0 |
| repl.py | 命令解析 | P1 |
| utils/validators.py | 参数验证 | P0 |
### 2.2 命令测试
| 命令 | 测试场景 | 优先级 |
|-----|---------|-------|
| project new | 正常创建 | P0 |
| project new | 参数验证错误 | P0 |
| project new | 文件已存在 | P1 |
| image new | 正常创建 | P0 |
| image scale | 缩放计算 | P0 |
| filter apply | 滤镜参数 | P1 |
## 3. E2E 测试计划
### 3.1 完整工作流测试
| 场景 | 步骤 | 验证点 |
|-----|------|-------|
| 创建-编辑-导出 | 1. project new → 2. image new → 3. filter apply → 4. export render | 文件生成正确 |
| 撤销-重做 | 1. 创建图层 → 2. 撤销 → 3. 重做 → 4. 验证状态 | 状态恢复正确 |
| 批量处理 | 1. batch 命令 → 2. 处理多个文件 → 3. 验证输出 | 所有文件处理成功 |
## 4. 测试数据
- 测试图片: test/data/sample.png (1920x1080)
- 测试项目: test/data/template.json
- 预期输出: test/expected/
## 5. 执行计划
```bash
# 运行所有测试
pytest -v
# 仅运行单元测试
pytest tests/unit/ -v
# 仅运行 E2E 测试
pytest tests/e2e/ -v
# 生成覆盖率报告
pytest --cov=cli_anything --cov-report=html
## 测试用例设计原则
### AAA 模式
```python
def test_image_scale():
# Arrange: 准备
image = ImageState(width=100, height=100)
# Act: 执行
image.scale(width=200, height=200)
# Assert: 验证
assert image.width == 200
assert image.height == 200
边界值测试
@pytest.mark.parametrize("width,height", [
(1, 1), # 最小值
(10000, 10000), # 最大值
(1920, 1080), # 典型值
])
def test_image_new_dimensions(width, height):
image = ImageState(width=width, height=height)
assert image.width == width
assert image.height == height
错误场景测试
def test_image_scale_invalid_dimensions():
image = ImageState(width=100, height=100)
with pytest.raises(ValueError) as exc_info:
image.scale(width=-1, height=100)
assert "width must be positive" in str(exc_info.value)
小结
Plan Tests 阶段确保测试全面覆盖:
- TEST.md - 结构化测试计划文档
- 单元测试 - 覆盖核心模块和命令
- E2E 测试 - 验证完整工作流
- 覆盖率目标 - 80% 以上代码覆盖率
系列导航:
- ← 上一篇:教程 4:第三阶段 - CLI 实现
- → 下一篇:教程 6:第五阶段 - 测试编写