name: risk-based-testing description: “使用风险评估和优先级划分,将测试工作集中在最高风险区域。适用于规划测试策略、分配测试资源或做出覆盖决策。” category: testing-methodologies priority: high tokenEstimate: 1000 agents: [qe-regression-risk-analyzer, qe-test-generator, qe-production-intelligence, qe-quality-gate] implementation_status: optimized optimization_version: 1.0 last_optimized: 2025-12-02 dependencies: [] quick_reference_card: true tags: [risk, prioritization, test-planning, coverage, impact-analysis] trust_tier: 3 validation: schema_path: schemas/output.json validator_path: scripts/validate-config.json eval_path: evals/risk-based-testing.yaml
基于风险的测试
<default_to_action> 当规划测试或分配测试资源时:
- 识别风险:什么可能出错?影响是什么?可能性有多大?
- 计算风险:风险 = 概率 × 影响(每个使用 1-5 等级)
- 优先排序:关键 (20+) → 高 (12-19) → 中 (6-11) → 低 (1-5)
- 分配工作量:60% 关键,25% 高,10% 中,5% 低
- 持续重新评估:新信息、变更、生产事件
快速风险评估:
- 概率因素:复杂性、变更频率、开发人员经验、技术债务
- 影响因素:用户数量、收入、安全、声誉、监管
- 动态调整:生产缺陷增加风险;稳定代码减少风险
关键成功因素:
- 在缺陷伤害最大的地方测试,而不是平均分配
- 风险是动态的 - 用新信息重新评估
- 生产数据告知风险(右移反馈左移) </default_to_action>
快速参考卡
使用时机
- 规划冲刺/发布测试策略
- 决定首先自动化什么
- 分配有限测试时间
- 证明测试覆盖决策
风险计算
Risk Score = Probability (1-5) × Impact (1-5)
| 分数 | 优先级 | 工作量 | 行动 |
|---|---|---|---|
| 20-25 | 关键 | 60% | 全面测试,多种技术 |
| 12-19 | 高 | 25% | 彻底测试,自动化优先 |
| 6-11 | 中 | 10% | 标准测试,基本自动化 |
| 1-5 | 低 | 5% | 冒烟测试,仅探索性 |
概率因素
| 因素 | 低 (1) | 中 (3) | 高 (5) |
|---|---|---|---|
| 复杂性 | 简单 CRUD | 业务逻辑 | 算法、集成 |
| 变更率 | 稳定 6+ 个月 | 月度变更 | 每周/每日变更 |
| 开发人员经验 | 高级,领域专家 | 中级 | 初级,新代码库 |
| 技术债务 | 干净代码 | 有些债务 | 遗留,无测试 |
影响因素
| 因素 | 低 (1) | 中 (3) | 高 (5) |
|---|---|---|---|
| 受影响的用户 | 仅管理员 | 部门 | 所有用户 |
| 收入 | 无 | 间接 | 直接(结账) |
| 安全 | 便利性 | 数据丢失 | 物理伤害 |
| 声誉 | 内部 | 行业 | 公众丑闻 |
风险评估工作流程
步骤 1: 列出功能/组件
Feature | Probability | Impact | Risk | Priority
--------|-------------|--------|------|----------
Checkout | 4 | 5 | 20 | Critical
User Auth | 3 | 5 | 15 | High
Admin Panel | 2 | 2 | 4 | Low
Search | 3 | 3 | 9 | Medium
步骤 2: 应用测试深度
await Task("Risk-Based Test Generation", {
critical: {
features: ['checkout', 'payment'],
depth: 'comprehensive',
techniques: ['unit', 'integration', 'e2e', 'performance', 'security']
},
high: {
features: ['auth', 'user-profile'],
depth: 'thorough',
techniques: ['unit', 'integration', 'e2e']
},
medium: {
features: ['search', 'notifications'],
depth: 'standard',
techniques: ['unit', 'integration']
},
low: {
features: ['admin-panel', 'settings'],
depth: 'smoke',
techniques: ['smoke-tests']
}
}, "qe-test-generator");
步骤 3: 动态重新评估
// 生产事件增加风险
await Task("Update Risk Score", {
feature: 'search',
event: 'production-incident',
previousRisk: 9,
newProbability: 5, // 由于事件增加
newRisk: 15 // 现在高优先级
}, "qe-regression-risk-analyzer");
ML 增强风险分析
// 代理使用历史数据预测风险
const riskAnalysis = await Task("ML Risk Analysis", {
codeChanges: changedFiles,
historicalBugs: bugDatabase,
prediction: {
model: 'gradient-boosting',
factors: ['complexity', 'change-frequency', 'author-experience', 'file-age']
}
}, "qe-regression-risk-analyzer");
// 输出:每个文件 95% 准确率风险预测
代理协调提示
内存命名空间
aqe/risk-based/
├── risk-scores/* - 当前风险评估
├── historical-bugs/* - 区域错误模式
├── production-data/* - 风险事件数据
└── coverage-map/* - 按风险级别测试深度
舰队协调
const riskFleet = await FleetManager.coordinate({
strategy: 'risk-based-testing',
agents: [
'qe-regression-risk-analyzer', // 风险评分
'qe-test-generator', // 风险适宜测试
'qe-production-intelligence', // 生产反馈
'qe-quality-gate' // 基于风险的门
],
topology: 'sequential'
});
与 CI/CD 集成
# 管道中基于风险的测试选择
- name: Risk Analysis
run: aqe risk-analyze --changes ${{ github.event.pull_request.files }}
- name: Run Critical Tests
if: risk.critical > 0
run: npm run test:critical
- name: Run High Tests
if: risk.high > 0
run: npm run test:high
- name: Skip Low Risk
if: risk.low_only
run: npm run test:smoke
相关技能
- agentic-quality-engineering - 风险感知代理
- context-driven-testing - 上下文影响风险
- regression-testing - 基于风险的回归选择
- shift-right-testing - 生产告知风险
记住
风险 = 概率 × 影响。 在缺陷伤害最大的地方测试。关键得到 60%,低得到 5%。风险是动态的 - 用新信息重新评估。生产事件提高风险分数。
使用代理: 代理使用 ML 基于历史数据计算风险,选择风险适宜的测试,并根据生产反馈调整分数。使用代理以规模维持动态风险档案。