关于 test-automator
test-automator 是面向开发者的技能,用于创建和维护多种语言及框架的自动化测试。内容涵盖单元、集成和端到端测试,以及覆盖率与测试设计实践。
主要功能
- 01单元、集成及端到端测试指南
- 02覆盖率目标与报告命令
- 03测试命名和设计原则
- 04针对框架的示例
- 05生成测试模板和报告的脚本
适用场景
- 01为新功能规划测试
- 02提升现有项目的覆盖率
- 03制定集成及端到端测试实践
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ zhaono1/ agent-playbook --skill test-automator安装后在 Claude Code 中输入/test-automator确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
README.mdreferences/best-practices.mdmocking.mdreferences/examples/README.mdunit-test-example.mdscripts/coverage_report.pygenerate_test.py详细介绍
测试自动化工具
精通为各种框架和语言创建与维护自动化测试。
此技能的触发条件
在以下情况下触发:
- 请求编写测试
- 提及测试自动化
- 请求提高测试覆盖率
- 需要设置测试框架
测试金字塔
/\
/E2E\ - Few, expensive, slow
/------\
/ Integration \ - Moderate number
/--------------\
/ Unit Tests \ - Many, cheap, fast
/------------------\
单元测试
原则
- 测试行为,而非实现
- 每个测试只做一个断言(通常如此)
- Arrange-Act-Assert 模式
- 使用描述性的测试名称
示例(Jest)
describe('UserService', () => {
describe('createUser', () => {
it('should create a user with valid data', async () => {
// Arrange
const userData = {
name: 'John Doe',
email: '[email protected]'
};
// 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');
});
});
});
集成测试
原则
- 测试组件之间的交互
- 使用测试替身模拟外部服务
- 清理测试数据
- 隔离运行
示例(Supertest)
describe('POST /api/users', () => {
it('should create a user', async () => {
const response = await request(app)
.post('/api/users')
.send({
name: 'John Doe',
email: '[email protected]'
})
.expect(201)
.expect((res) => {
expect(res.body.id).toBeDefined();
expect(res.body.email).toBe('[email protected]');
});
});
});
端到端测试
原则
- 测试关键用户流程
- 使用真实的测试数据
- 妥善处理异步操作
- 测试后进行清理
示例(Playwright)
test('user can login', async ({ page }) => {
await page.goto('/login');
await page.fill('[name="email"]', '[email protected]');
await page.fill('[name="password"]', 'password123');
await page.click('button[type="submit"]');
await expect(page).toHaveURL('/dashboard');
await expect(page.locator('h1')).toContainText('Welcome');
});
测试覆盖率
覆盖率目标
| 类型 | 目标 |
|---|---|
| 行 | > 80% |
| 分支 | > 75% |
| 函数 | > 80% |
| 语句 | > 80% |
覆盖率报告
# Jest
npm test -- --coverage
# Python (pytest-cov)
pytest --cov=src --cov-report=html
# Go
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
测试最佳实践
应做事项
- 在修复错误前编写测试(TDD)
- 测试边界情况
- 保持测试相互独立
- 使用描述性的测试名称
- 模拟外部依赖
- 清理测试数据
不应做事项
- 不要测试实现细节
- 不要编写脆弱的测试
- 没有理由时不要跳过测试
- 不要提交已注释掉的测试
- 不要测试第三方库
测试命名约定
// 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/JS | Jest、Vitest | npm test |
| Python | pytest | pytest |
| Go | testing | go test |
| Java | JUnit | mvn test |
| Rust | 内置 | cargo test |
脚本
生成测试样板代码:
python3 scripts/generate_test.py --name <feature> --output tests/test-plan.md
检查测试覆盖率:
python3 scripts/coverage_report.py --name <service-name> --output coverage-report.md
参考资料
references/best-practices.md- 测试最佳实践references/examples/- 特定框架的示例references/mocking.md- 模拟指南
评论
支持 Markdown