mcpField
T
testing-strategy

testing-strategy

github.com/block测试与质量25151 次安装18 周前更新
代码仓库

关于 testing-strategy

面向开发者的软件测试指南,涵盖单元测试、集成测试、测试目录组织、运行方式及测试质量实践。

主要功能

  1. 01单元测试隔离建议
  2. 02集成测试建议
  3. 03测试运行命令示例
  4. 04测试目录结构组织
  5. 05Arrange-Act-Assert 实践
  6. 06覆盖率优先级

适用场景

  1. 01为单个行为规划单元测试
  2. 02测试 API 端点和数据库变更
  3. 03组织共享 fixture 与测试辅助工具
  4. 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

最佳实践

  1. Arrange-Act-Assert:清晰地组织测试
  2. 每个测试一个断言:尽可能只测试一种行为
  3. 描述性名称:测试名称应描述场景
  4. 测试之间不相互依赖:测试应能以任意顺序运行
  5. 状态干净:每个测试都从已知状态开始

覆盖率目标

  • 目标是达到 80% 以上的行覆盖率
  • 优先关注关键路径
  • 不要为了覆盖率数字牺牲测试质量

评论

支持 Markdown