关于 qe-test-automation-strategy
面向测试自动化方案设计与改进的技能,介绍测试金字塔、F.I.R.S.T. 原则、常见设计模式和 CI/CD 集成,也涵盖易波动测试的管理。
主要功能
- 01按测试金字塔规划测试层级
- 02应用 F.I.R.S.T. 原则
- 03使用页面对象等设计模式
- 04集成 CI/CD 流程
- 05隔离和处理易波动测试
适用场景
- 01设计新的自动化测试框架
- 02缩短测试套件的执行时间
- 03降低易波动测试的影响
- 04改进 CI/CD 测试反馈
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ proffesor-for-testing/ agentic-qe --skill qe-test-automation-strategy安装后在 Claude Code 中输入/qe-test-automation-strategy确认已加载。
详细介绍
测试自动化策略
<default_to_action> 设计或改进测试自动化时:
- 遵循测试金字塔:70% 单元测试、20% 集成测试、10% E2E 测试
- 应用 F.I.R.S.T. 原则:快速(Fast)、独立(Isolated)、可重复(Repeatable)、自验证(Self-validating)、及时(Timely)
- 使用以下模式:页面对象模型(Page Object Model)、建造者模式(Builder pattern)、工厂模式(Factory pattern)
- 集成到 CI/CD:每次提交都运行测试、快速失败、提供清晰反馈
- 管理不稳定测试:隔离、修复或删除,绝不忽略
快速反模式检测:
- 冰淇淋甜筒(E2E 测试多、单元测试少)→ 调整为金字塔
- 测试缓慢(完整套件超过 10 分钟)→ 并行运行、模拟外部依赖
- 不稳定测试 → 修复时序问题、隔离数据或将其隔离
- 测试重复 → 共用测试夹具、使用页面对象
- 脆弱的选择器 → 使用 data-testid、语义定位器
关键成功因素:
- 目标是快速反馈(完整套件少于 10 分钟)
- 自动化为测试提供支持,而非取代判断
- 像对待生产代码一样投入测试基础设施
</default_to_action>
快速参考卡
适用场景
- 构建新的自动化框架
- 提高现有测试效率
- 减轻不稳定测试带来的负担
- 优化 CI/CD 流水线速度
测试金字塔
| 层级 | % | 速度 | 隔离性 | 示例 |
|---|---|---|---|---|
| 单元测试 | 70% | < 1ms | 完全 | 纯函数、逻辑 |
| 集成测试 | 20% | < 1s | 部分 | API、数据库 |
| E2E 测试 | 10% | < 30s | 无 | 用户旅程 |
F.I.R.S.T. 原则
| 原则 | 含义 | 做法 |
|---|---|---|
| Fast | 快速执行 | 模拟外部依赖 |
| Isolated | 无共享状态 | 每项测试使用全新测试夹具 |
| Repeatable | 每次结果相同 | 不使用随机数据 |
| Self-validating | 明确通过或失败 | 使用断言,而非打印 |
| Timely | 与代码同步编写 | 采用 TDD,而非事后编写 |
反模式
| 问题 | 症状 | 修复方法 |
|---|---|---|
| 冰淇淋甜筒 | 80% E2E、10% 单元测试 | 调整为金字塔 |
| 测试套件缓慢 | CI 耗时 30 分钟以上 | 并行运行、精简测试 |
| 不稳定测试 | 随机失败 | 隔离、修复时序问题 |
| 测试耦合 | 依赖执行顺序 | 隔离数据 |
| 脆弱的选择器 | CSS 变更就会失效 | 使用 data-testid |
页面对象模型
// pages/LoginPage.js
class LoginPage {
constructor(page) {
this.page = page;
this.emailInput = '[data-testid="email"]';
this.passwordInput = '[data-testid="password"]';
this.submitButton = '[data-testid="submit"]';
this.errorMessage = '[data-testid="error"]';
}
async login(email, password) {
await this.page.fill(this.emailInput, email);
await this.page.fill(this.passwordInput, password);
await this.page.click(this.submitButton);
}
async getError() {
return this.page.textContent(this.errorMessage);
}
}
// Test uses page object
test('shows error for invalid credentials', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.login('[email protected]', 'wrong');
expect(await loginPage.getError()).toBe('Invalid credentials');
});
CI/CD 集成
name: Test Pipeline
on: [push, pull_request]
jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:unit -- --coverage
timeout-minutes: 5
- uses: codecov/codecov-action@v3
integration-tests:
needs: unit-tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
steps:
- run: npm run test:integration
timeout-minutes: 10
e2e-tests:
needs: integration-tests
runs-on: ubuntu-latest
steps:
- run: npx playwright test
timeout-minutes: 15
不稳定测试管理
// Quarantine flaky tests
describe.skip('Quarantined - INC-123', () => {
test('flaky test awaiting fix', () => { /* ... */ });
});
// Agent-assisted stabilization
await Task("Fix Flaky Tests", {
tests: quarantinedTests,
analysis: ['timing-issues', 'data-isolation', 'race-conditions'],
strategies: ['add-waits', 'isolate-fixtures', 'mock-externals']
}, "qe-flaky-test-hunter");
Agent 辅助自动化
// Generate tests following pyramid
await Task("Generate Test Suite", {
sourceCode: 'src/',
pyramid: { unit: 70, integration: 20, e2e: 10 },
patterns: ['page-object', 'builder', 'factory'],
framework: 'jest'
}, "qe-test-generator");
// Optimize test execution
await Task("Optimize Suite", {
algorithm: 'johnson-lindenstrauss',
targetReduction: 0.3,
maintainCoverage: 0.95
}, "qe-regression-risk-analyzer");
// Analyze flaky patterns
await Task("Flaky Analysis", {
testHistory: 'last-30-days',
detectPatterns: ['timing', 'data', 'environment'],
recommend: 'stabilization-strategy'
}, "qe-flaky-test-hunter");
Agent 协调提示
记忆命名空间
aqe/automation/
├── test-pyramid/* - Coverage by layer
├── page-objects/* - Shared page objects
├── flaky-registry/* - Quarantined tests
└── execution-metrics/* - Suite performance data
集群协调
const automationFleet = await FleetManager.coordinate({
strategy: 'test-automation',
agents: [
'qe-test-generator', // Generate pyramid-compliant tests
'qe-test-executor', // Parallel execution
'qe-coverage-analyzer', // Coverage gaps
'qe-flaky-test-hunter', // Flaky detection
'qe-regression-risk-analyzer' // Smart selection
],
topology: 'hierarchical'
});
相关技能
- tdd-london-chicago - 用于单元测试的 TDD
- api-testing-patterns - 集成模式
- cicd-pipeline-qe-orchestrator - 流水线集成
- shift-left-testing - 尽早开展自动化
请牢记
金字塔:70% 单元测试、20% 集成测试、10% E2E 测试。 每项测试都要遵循 F.I.R.S.T. 原则。E2E 测试使用页面对象模型。通过并行运行提升速度。隔离不稳定测试,绝不忽略。像对待生产代码一样对待测试代码。
使用 Agent: Agent 可以生成符合金字塔原则的测试、检测不稳定模式、优化执行时间并维护测试基础设施。使用 Agent 提升自动化质量。
评论
支持 Markdown