testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTesting Best Practices
测试最佳实践
Test Structure (AAA Pattern)
测试结构(AAA Pattern)
- Arrange: Set up test data and conditions
- Act: Execute the code under test
- Assert: Verify the expected outcome
- Use descriptive test names: "should X when Y"
- 准备(Arrange):设置测试数据和条件
- 执行(Act):运行待测试代码
- 断言(Assert):验证预期结果
- 使用描述性测试名称:"当Y时应该X"
Unit Tests
单元测试
- Test one behavior per test
- Mock external dependencies
- Test edge cases and error paths
- Keep tests fast (< 100ms each)
- Use factories for test data
- Aim for 80%+ coverage
- 每个测试仅验证一种行为
- 模拟外部依赖
- 测试边界情况和错误路径
- 保持测试快速(每个测试<100毫秒)
- 使用工厂函数生成测试数据
- 目标覆盖率达80%以上
Integration Tests
集成测试
- Test component interactions
- Use test database/containers
- Clean up after tests
- Test happy and error paths
- Use realistic data
- 测试组件间的交互
- 使用测试数据库/容器
- 测试后清理环境
- 测试正常流程和错误路径
- 使用真实场景的数据
E2E Tests
E2E测试
- Test critical user flows only
- Keep tests independent
- Use stable selectors (data-testid)
- Handle async properly
- Run in CI with retries
- 仅测试关键用户流程
- 保持测试独立性
- 使用稳定的选择器(data-testid)
- 正确处理异步操作
- 在CI中运行并配置重试机制
Mocking
模拟(Mocking)
- Mock at boundaries (APIs, DB)
- Use factories for test data
- Don't over-mock (test real behavior)
- Reset mocks between tests
- Prefer fakes over mocks when possible
- 在边界处进行模拟(API、数据库)
- 使用工厂函数生成测试数据
- 不要过度模拟(测试真实行为)
- 测试间重置模拟对象
- 可能的话优先使用假实现(fakes)而非模拟(mocks)
Tools
工具
- Jest/Vitest for unit tests
- Playwright/Cypress for E2E
- MSW for API mocking
- Faker for test data
- Testing Library for component tests
- Jest/Vitest 用于单元测试
- Playwright/Cypress 用于E2E测试
- MSW 用于API模拟
- Faker 用于生成测试数据
- Testing Library 用于组件测试