mcpField
Q
qe-mutation-testing

qe-mutation-testing

github.com/proffesor-for-testing测试与质量51981 次安装今天更新
代码仓库

关于 qe-mutation-testing

面向测试工程师和开发者的变异测试技能,通过修改代码并运行测试,判断测试是否能捕获缺陷。它还指导分析存活变异体并补强测试。

主要功能

  1. 01修改运算符或移除语句生成变异体
  2. 02运行测试并统计杀死与存活变异体
  3. 03定位测试断言薄弱处
  4. 04指导补充测试以消除存活变异体
  5. 05提供 Stryker 配置与命令参考

适用场景

  1. 01评估测试套件能否捕获缺陷
  2. 02检查关键代码路径的测试断言
  3. 03发布前查找测试质量缺口

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/proffesor-for-testing/agentic-qe --skill qe-mutation-testing

安装后在 Claude Code 中输入/qe-mutation-testing确认已加载。

详细介绍

变异测试

<default_to_action> 在验证测试质量或提升测试效果时:

  1. 对代码进行变异(将 + 改为 -,将 >= 改为 >,删除语句)
  2. 针对每个变异体运行测试
  3. 验证测试能否捕获变异(杀死变异体)
  4. 找出存活的变异体(测试需要改进)
  5. 强化测试,以杀死存活的变异体

快速变异指标:

  • 变异分数 = 已杀死的变异体 /(已杀死的变异体 + 存活的变异体)
  • 目标:变异分数 > 80%
  • 存活的变异体 = 测试较弱

关键成功因素:

  • 高覆盖率 ≠ 好测试(100% 覆盖率,0% 断言)
  • 变异测试可以证明测试确实能捕获 Bug
  • 优先关注关键代码路径 </default_to_action>

快速参考卡

适用场景
  • 评估测试套件质量
  • 查找测试断言中的缺口
  • 证明测试能捕获 Bug
  • 重要版本发布前
变异分数解读
分数解读
90%+测试质量优秀
80-90%良好,仅需少量改进
60-80%需要关注
< 60%存在明显缺口
常见变异操作符
类别原始代码变异体
算术a + ba - b
关系x >= 18x > 18
逻辑a && ba || b
条件if (x)if (true)
语句return x(removed)

变异测试的工作原理

// Original code
function isAdult(age) {
  return age >= 18; // ← Mutant: change >= to >
}

// Strong test (catches mutation)
test('18 is adult', () => {
  expect(isAdult(18)).toBe(true); // Kills mutant!
});

// Weak test (mutation survives)
test('19 is adult', () => {
  expect(isAdult(19)).toBe(true); // Doesn't catch >= vs >
});
// Surviving mutant → Test needs boundary value

使用 Stryker

# Install
npm install --save-dev @stryker-mutator/core @stryker-mutator/jest-runner

# Initialize
npx stryker init

配置:

{
  "packageManager": "npm",
  "reporters": ["html", "clear-text", "progress"],
  "testRunner": "jest",
  "coverageAnalysis": "perTest",
  "mutate": [
    "src/**/*.ts",
    "!src/**/*.spec.ts"
  ],
  "thresholds": {
    "high": 90,
    "low": 70,
    "break": 60
  }
}

运行:

npx stryker run

输出:

Mutation Score: 87.3%
Killed: 124
Survived: 18
No Coverage: 3
Timeout: 1

修复存活的变异体

// Surviving mutant: >= changed to >
function calculateDiscount(quantity) {
  if (quantity >= 10) { // Mutant survives!
    return 0.1;
  }
  return 0;
}

// Original weak test
test('large order gets discount', () => {
  expect(calculateDiscount(15)).toBe(0.1); // Doesn't test boundary
});

// Fixed: Add boundary test
test('exactly 10 gets discount', () => {
  expect(calculateDiscount(10)).toBe(0.1); // Kills mutant!
});

test('9 does not get discount', () => {
  expect(calculateDiscount(9)).toBe(0); // Tests below boundary
});

由 Agent 驱动的变异测试

// Analyze mutation score and generate fixes
await Task("Mutation Analysis", {
  targetFile: 'src/payment.ts',
  generateMissingTests: true,
  minScore: 80
}, "qe-test-generator");

// Returns:
// {
//   mutationScore: 0.65,
//   survivedMutations: [
//     { line: 45, operator: '>=', mutant: '>', killedBy: null }
//   ],
//   generatedTests: [
//     'test for boundary at line 45'
//   ]
// }

// Coverage + mutation correlation
await Task("Coverage Quality Analysis", {
  coverageData: coverageReport,
  mutationData: mutationReport,
  identifyWeakCoverage: true
}, "qe-coverage-analyzer");

Agent 协作提示

记忆命名空间
aqe/mutation-testing/
├── mutation-results/*   - Stryker reports
├── surviving/*          - Surviving mutants
├── generated-tests/*    - Tests to kill mutants
└── trends/*             - Mutation score over time
集群协作
const mutationFleet = await FleetManager.coordinate({
  strategy: 'mutation-testing',
  agents: [
    'qe-test-generator',     // Generate tests for survivors
    'qe-coverage-analyzer',  // Coverage correlation
    'qe-quality-analyzer'    // Quality assessment
  ],
  topology: 'sequential'
});

相关技能


请记住

高代码覆盖率 ≠ 好测试。 100% 覆盖率但断言较弱,等于毫无用处。变异测试可以证明测试确实能捕获 Bug。

优先关注关键路径。 不要对所有代码都进行变异测试,应优先处理支付、身份验证和数据完整性相关代码。

使用 Agent: Agent 会运行变异分析、找出存活的变异体,并生成缺失的测试用例来杀死它们,从而自动提升测试质量。

评论

支持 Markdown