关于 testing-strategy
面向开发者的软件测试指南,涵盖单元测试、集成测试、测试目录组织、运行方式及测试质量实践。
主要功能
- 01单元测试隔离建议
- 02集成测试建议
- 03测试运行命令示例
- 04测试目录结构组织
- 05Arrange-Act-Assert 实践
- 06覆盖率优先级
适用场景
- 01为单个行为规划单元测试
- 02测试 API 端点和数据库变更
- 03组织共享 fixture 与测试辅助工具
- 04优先覆盖关键路径
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ block/ agent-skills --skill testing-strategy安装后在 Claude Code 中输入/testing-strategy确认已加载。
详细介绍
测试指南
单元测试
- 每个测试只测试一件事
- 使用描述性测试名称:
test_user_creation_fails_with_invalid_email - 模拟外部依赖
- 保持测试快速且相互隔离
集成测试
- 使用真实的数据测试 API 端点
- 验证数据库状态变更
- 在每个测试后清理测试数据
- 使用测试夹具处理常见场景
运行测试
# Run all tests
npm test
# Run unit tests only
npm test:unit
# Run integration tests (requires database)
npm test:integration
# Run tests with coverage
npm test:coverage
测试结构
tests/
├── unit/ # Fast, isolated unit tests
├── integration/ # Tests requiring external services
├── fixtures/ # Shared test data
└── helpers/ # Test utilities
最佳实践
- Arrange-Act-Assert:清晰地组织测试
- 每个测试一个断言:尽可能只测试一种行为
- 描述性名称:测试名称应描述场景
- 测试之间不相互依赖:测试应能以任意顺序运行
- 状态干净:每个测试都从已知状态开始
覆盖率目标
- 目标是达到 80% 以上的行覆盖率
- 优先关注关键路径
- 不要为了覆盖率数字牺牲测试质量
评论
支持 Markdown