test-automator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTest Automator
测试自动化专家
Expert in creating and maintaining automated tests for various frameworks and languages.
擅长为各类框架和语言创建并维护自动化测试。
When This Skill Activates
该技能触发场景
Activates when you:
- Ask to write tests
- Mention test automation
- Request test coverage improvement
- Need to set up testing framework
当你进行以下操作时触发:
- 要求编写测试
- 提及测试自动化
- 请求提升测试覆盖率
- 需要搭建测试框架
Testing Pyramid
测试金字塔
/\
/E2E\ - Few, expensive, slow
/------\
/ Integration \ - Moderate number
/--------------\
/ Unit Tests \ - Many, cheap, fast
/------------------\ /\
/E2E\ - 数量少、成本高、速度慢
/------\
/ Integration \ - 数量适中
/--------------\
/ Unit Tests \ - 数量多、成本低、速度快
/------------------\Unit Testing
单元测试
Principles
原则
- Test behavior, not implementation
- One assertion per test (generally)
- Arrange-Act-Assert pattern
- Descriptive test names
- 测试行为,而非实现细节
- 每个测试通常仅包含一个断言
- Arrange-Act-Assert(准备-执行-断言)模式
- 描述性的测试名称
Example (Jest)
示例(Jest)
typescript
describe('UserService', () => {
describe('createUser', () => {
it('should create a user with valid data', async () => {
// Arrange
const userData = {
name: 'John Doe',
email: 'john@example.com'
};
// Act
const user = await userService.create(userData);
// Assert
expect(user.id).toBeDefined();
expect(user.email).toBe(userData.email);
});
it('should throw error for invalid email', async () => {
// Arrange
const userData = { email: 'invalid' };
// Act & Assert
await expect(userService.create(userData))
.rejects.toThrow('Invalid email');
});
});
});typescript
describe('UserService', () => {
describe('createUser', () => {
it('should create a user with valid data', async () => {
// Arrange
const userData = {
name: 'John Doe',
email: 'john@example.com'
};
// Act
const user = await userService.create(userData);
// Assert
expect(user.id).toBeDefined();
expect(user.email).toBe(userData.email);
});
it('should throw error for invalid email', async () => {
// Arrange
const userData = { email: 'invalid' };
// Act & Assert
await expect(userService.create(userData))
.rejects.toThrow('Invalid email');
});
});
});Integration Testing
集成测试
Principles
原则
- Test component interactions
- Use test doubles for external services
- Clean up test data
- Run in isolation
- 测试组件间的交互
- 为外部服务使用测试替身(Test Doubles)
- 清理测试数据
- 独立运行测试
Example (Supertest)
示例(Supertest)
typescript
describe('POST /api/users', () => {
it('should create a user', async () => {
const response = await request(app)
.post('/api/users')
.send({
name: 'John Doe',
email: 'john@example.com'
})
.expect(201)
.expect((res) => {
expect(res.body.id).toBeDefined();
expect(res.body.email).toBe('john@example.com');
});
});
});typescript
describe('POST /api/users', () => {
it('should create a user', async () => {
const response = await request(app)
.post('/api/users')
.send({
name: 'John Doe',
email: 'john@example.com'
})
.expect(201)
.expect((res) => {
expect(res.body.id).toBeDefined();
expect(res.body.email).toBe('john@example.com');
});
});
});E2E Testing
E2E测试
Principles
原则
- Test critical user flows
- Use realistic test data
- Handle async operations properly
- Clean up after tests
- 测试关键用户流程
- 使用真实的测试数据
- 正确处理异步操作
- 测试后清理环境
Example (Playwright)
示例(Playwright)
typescript
test('user can login', async ({ page }) => {
await page.goto('/login');
await page.fill('[name="email"]', 'user@example.com');
await page.fill('[name="password"]', 'password123');
await page.click('button[type="submit"]');
await expect(page).toHaveURL('/dashboard');
await expect(page.locator('h1')).toContainText('Welcome');
});typescript
test('user can login', async ({ page }) => {
await page.goto('/login');
await page.fill('[name="email"]', 'user@example.com');
await page.fill('[name="password"]', 'password123');
await page.click('button[type="submit"]');
await expect(page).toHaveURL('/dashboard');
await expect(page.locator('h1')).toContainText('Welcome');
});Test Coverage
测试覆盖率
Coverage Goals
覆盖率目标
| Type | Target |
|---|---|
| Lines | > 80% |
| Branches | > 75% |
| Functions | > 80% |
| Statements | > 80% |
| 类型 | 目标 |
|---|---|
| 代码行 | > 80% |
| 分支 | > 75% |
| 函数 | > 80% |
| 语句 | > 80% |
Coverage Reports
覆盖率报告
bash
undefinedbash
undefinedJest
Jest
npm test -- --coverage
npm test -- --coverage
Python (pytest-cov)
Python (pytest-cov)
pytest --cov=src --cov-report=html
pytest --cov=src --cov-report=html
Go
Go
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
undefinedgo test -coverprofile=coverage.out
go tool cover -html=coverage.out
undefinedTesting Best Practices
测试最佳实践
DO's
建议事项
- Write tests before fixing bugs (TDD)
- Test edge cases
- Keep tests independent
- Use descriptive test names
- Mock external dependencies
- Clean up test data
- 修复Bug前先编写测试(TDD)
- 测试边缘情况
- 保持测试独立性
- 使用描述性的测试名称
- 模拟外部依赖
- 清理测试数据
DON'Ts
禁忌事项
- Don't test implementation details
- Don't write brittle tests
- Don't skip tests without a reason
- Don't commit commented-out tests
- Don't test third-party libraries
- 不要测试实现细节
- 不要编写脆弱的测试
- 无正当理由不要跳过测试
- 不要提交被注释掉的测试
- 不要测试第三方库
Test Naming Conventions
测试命名规范
typescript
// Good: Describes what is being tested
it('should reject invalid email addresses')
// Good: Describes the scenario and outcome
it('returns 401 when user provides invalid credentials')
// Bad: Vague
it('works correctly')typescript
// Good: Describes what is being tested
it('should reject invalid email addresses')
// Good: Describes the scenario and outcome
it('returns 401 when user provides invalid credentials')
// Bad: Vague
it('works correctly')Common Testing Frameworks
常用测试框架
| Language | Framework | Command |
|---|---|---|
| TypeScript/JS | Jest, Vitest | |
| Python | pytest | |
| Go | testing | |
| Java | JUnit | |
| Rust | built-in | |
| 语言 | 框架 | 命令 |
|---|---|---|
| TypeScript/JS | Jest, Vitest | |
| Python | pytest | |
| Go | testing | |
| Java | JUnit | |
| Rust | 内置测试框架 | |
Scripts
脚本工具
Generate test boilerplate:
bash
python scripts/generate_test.py <filename>Check test coverage:
bash
python scripts/coverage_report.py生成测试模板:
bash
python scripts/generate_test.py <filename>检查测试覆盖率:
bash
python scripts/coverage_report.pyReferences
参考资料
- - Testing best practices
references/best-practices.md - - Framework-specific examples
references/examples/ - - Mocking guidelines
references/mocking.md
- - 测试最佳实践
references/best-practices.md - - 框架专属示例
references/examples/ - - 模拟指南
references/mocking.md