testing-strategy

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Testing Guidelines

测试指南

Unit Tests

单元测试

  • Test one thing per test
  • Use descriptive test names:
    test_user_creation_fails_with_invalid_email
  • Mock external dependencies
  • Keep tests fast and isolated
  • 每个测试仅验证一个功能点
  • 使用描述性的测试名称:
    test_user_creation_fails_with_invalid_email
  • 模拟外部依赖
  • 保持测试快速且独立

Integration Tests

集成测试

  • Test API endpoints with realistic data
  • Verify database state changes
  • Clean up test data after each test
  • Use test fixtures for common scenarios
  • 使用真实数据测试API端点
  • 验证数据库状态变化
  • 每次测试后清理测试数据
  • 针对常见场景使用测试夹具(fixtures)

Running Tests

运行测试

bash
undefined
bash
undefined

Run all tests

Run all tests

npm test
npm test

Run unit tests only

Run unit tests only

npm test:unit
npm test:unit

Run integration tests (requires database)

Run integration tests (requires database)

npm test:integration
npm test:integration

Run tests with coverage

Run tests with coverage

npm test:coverage
undefined
npm test:coverage
undefined

Test Structure

测试结构

tests/
├── unit/           # Fast, isolated unit tests
├── integration/    # Tests requiring external services
├── fixtures/       # Shared test data
└── helpers/        # Test utilities
tests/
├── unit/           # 快速、独立的单元测试
├── integration/    # 需要外部服务的测试
├── fixtures/       # 共享测试数据
└── helpers/        # 测试工具

Best Practices

最佳实践

  1. Arrange-Act-Assert: Structure tests clearly
  2. One assertion per test: When possible, test one behavior
  3. Descriptive names: Test names should describe the scenario
  4. No test interdependence: Tests should run in any order
  5. Clean state: Each test starts with a known state
  1. 准备-执行-断言(Arrange-Act-Assert):清晰地组织测试结构
  2. 每个测试尽量仅一个断言:尽可能只测试一种行为
  3. 描述性命名:测试名称应说明测试场景
  4. 测试无依赖:测试可以按任意顺序运行
  5. 干净状态:每个测试都从已知状态开始

Coverage Goals

覆盖率目标

  • Aim for 80%+ line coverage
  • Focus on critical paths first
  • Don't sacrifice test quality for coverage numbers
  • 目标达到80%以上的代码行覆盖率
  • 优先覆盖关键路径
  • 不要为了覆盖率数字牺牲测试质量