测试命令 (/test)
全面的测试工作流命令,支持单元测试、集成测试、E2E测试和性能测试。
核心功能
- 多层测试: 单元→集成→E2E→性能测试全覆盖
- 智能生成: 基于代码自动生成测试用例
- 框架适配: 支持Jest、Cypress、Playwright等主流框架
- 覆盖率分析: 详细的测试覆盖率报告
- 性能监控: 集成性能基准测试
使用语法
bash
/test [类型] [目标] [选项]测试类型
unit- 单元测试integration- 集成测试e2e- 端到端测试performance- 性能测试visual- 视觉回归测试accessibility- 无障碍测试
主要选项
--generate- 自动生成测试用例--coverage- 生成覆盖率报告--watch- 监听模式--ci- CI/CD集成模式--benchmark- 性能基准测试--parallel- 并行执行测试
测试类型详解
单元测试
bash
# 运行所有单元测试
/test unit --coverage
# 测试特定模块
/test unit src/auth/ --watch
# 生成测试用例
/test unit --generate src/utils/validator.js自动生成示例:
javascript
// 基于函数签名自动生成
describe('validateEmail', () => {
test('should return true for valid email', () => {
expect(validateEmail('test@example.com')).toBe(true);
});
test('should return false for invalid email', () => {
expect(validateEmail('invalid-email')).toBe(false);
});
test('should handle edge cases', () => {
expect(validateEmail('')).toBe(false);
expect(validateEmail(null)).toBe(false);
});
});集成测试
bash
# API集成测试
/test integration --api --environment=staging
# 数据库集成测试
/test integration --database --transactions
# 服务间集成测试
/test integration --services --contract-testingE2E测试
bash
# 用户流程测试
/test e2e --user-journey=login,purchase,checkout
# 跨浏览器测试
/test e2e --browsers=chrome,firefox,safari
# 移动端E2E测试
/test e2e --mobile --devices=iphone,android性能测试
bash
# 负载测试
/test performance --load --users=100 --duration=5m
# 压力测试
/test performance --stress --ramp-up=gradual
# 基准测试
/test performance --benchmark --baseline=previous智能测试生成
基于代码分析
bash
# 分析函数并生成测试
/test unit --generate --analyze-function getUserProfile
# 分析API端点并生成测试
/test integration --generate --analyze-endpoints /api/users/*
# 分析组件并生成测试
/test unit --generate --analyze-component UserCard.jsx基于需求生成
bash
# 基于用户故事生成E2E测试
/test e2e --generate --user-story "用户可以搜索产品并添加到购物车"
# 基于API文档生成集成测试
/test integration --generate --api-spec openapi.yaml覆盖率分析
详细报告
bash
# 生成HTML覆盖率报告
/test unit --coverage --format=html
# 生成覆盖率徽章
/test --coverage --badge --threshold=80
# 覆盖率趋势分析
/test --coverage --trend --compare=last-week覆盖率标准
| 类型 | 最低标准 | 推荐标准 | 优秀标准 |
|---|---|---|---|
| 语句覆盖率 | 70% | 85% | 95% |
| 分支覆盖率 | 60% | 80% | 90% |
| 函数覆盖率 | 80% | 90% | 100% |
| 行覆盖率 | 75% | 85% | 95% |
CI/CD集成
持续测试
bash
# CI模式运行
/test --ci --fail-fast --timeout=10m
# 生成JUnit报告
/test --ci --report=junit --output=test-results.xml
# 并行测试执行
/test --ci --parallel --max-workers=4测试管道
yaml
# 测试管道配置示例
test_pipeline:
stages:
- unit_tests: /test unit --coverage --ci
- integration_tests: /test integration --api --ci
- e2e_tests: /test e2e --critical-paths --ci
- performance_tests: /test performance --baseline --ci框架支持
JavaScript/TypeScript
- Jest: 单元和集成测试
- Cypress: E2E测试
- Playwright: 跨浏览器E2E测试
- Mocha + Chai: 传统测试框架
- Vitest: 快速单元测试
Python
- pytest: 综合测试框架
- unittest: 标准库测试
- Selenium: Web自动化测试
- locust: 性能测试
Java
- JUnit: 单元测试标准
- TestNG: 企业级测试框架
- Selenium: Web测试
- JMeter: 性能测试
高级特性
视觉回归测试
bash
# 截图对比测试
/test visual --component=Button --variants=all
# 跨设备视觉测试
/test visual --responsive --devices=mobile,tablet,desktop
# 主题对比测试
/test visual --themes=light,dark --components=critical无障碍测试
bash
# WCAG合规性测试
/test accessibility --standard=wcag21-aa
# 键盘导航测试
/test accessibility --keyboard-navigation
# 屏幕阅读器兼容测试
/test accessibility --screen-reader合约测试
bash
# API合约测试
/test contract --provider=user-service --consumer=web-app
# 数据库schema测试
/test contract --database --migration-safe
# 微服务通信测试
/test contract --services --pact-broker测试策略
测试金字塔
E2E Tests (10%)
────────────────────
Integration Tests (20%)
──────────────────────────
Unit Tests (70%)测试驱动开发(TDD)
bash
# TDD工作流
1. /test unit --generate --red # 编写失败测试
2. # 实现最小功能代码 # 让测试通过
3. /test unit --watch # 重构并保持测试通过行为驱动开发(BDD)
bash
# BDD场景测试
/test e2e --bdd --feature="用户登录功能"
/test e2e --gherkin --scenarios=login.feature自定义配置
测试配置文件
json
{
"test_preferences": {
"default_type": "unit",
"coverage_threshold": 85,
"parallel_execution": true,
"auto_generate": true,
"frameworks": {
"unit": "jest",
"e2e": "playwright",
"performance": "lighthouse"
}
}
}环境配置
yaml
test_environments:
development:
database: test_db_dev
api_url: http://localhost:3000
staging:
database: test_db_staging
api_url: https://api-staging.example.com
production:
database: test_db_prod
api_url: https://api.example.com最佳实践
1. 测试命名约定
javascript
// 好的测试命名
describe('UserService.createUser', () => {
test('should create user with valid data', () => {});
test('should throw error when email is invalid', () => {});
test('should handle duplicate email gracefully', () => {});
});
// 不好的测试命名
describe('UserService', () => {
test('test1', () => {});
test('user creation', () => {});
});2. 测试数据管理
bash
# 使用测试数据工厂
/test --setup-fixtures --factory-pattern
# 数据隔离
/test --database-transactions --rollback
# 模拟数据生成
/test --mock-data --realistic3. 测试维护
bash
# 定期清理废弃测试
/test --cleanup --unused-tests
# 测试性能监控
/test --monitor --slow-tests-threshold=5s
# 测试稳定性分析
/test --flaky-test-detection --retry=3工具集成
Claude Code工具
- Bash: 测试命令执行
- Read: 代码分析和测试生成
- Write: 测试文件创建
- TodoWrite: 测试进度跟踪
MCP服务
- Playwright: E2E测试执行
- Sequential: 复杂测试场景分析
- Context7: 测试框架最佳实践
测试命令 - 构建可靠软件的质量保障