# 变异测试

变异测试是一种软件测试技术，它通过向程序中注入被称为变异体的小型人工缺陷，并检查测试能否检测到这些缺陷，来衡量测试套件的强度。<sup>[1](https://mutationtesting.uni.lu/survey.pdf)</sup> 它评估的是测试本身，而非在生产代码中搜寻缺陷：杀死大多数变异体的测试套件被判定为合格的，被杀死的变异体比例即为变异得分。<sup>[2](https://softengbook.org/articles/mutation-testing)</sup> 与仅记录哪些代码被执行了的代码覆盖率不同，变异测试检验的是测试本身。<sup>[3](https://stryker-mutator.io/blog/introduction-to-mutation-testing/)</sup>

| 关键事实 | 详情 |
|---|---|
| 变异得分 | 测试套件杀死的非等价变异体所占比例，计算为 \( \mathrm{MS} = K/(M-E) \)，其中 \(K\) 为杀死的变异体数，\(M\) 为变异体总数，\(E\) 为等价变异体数；取值位于 \([0,1]\) 区间。<sup>[4](https://www.albany.edu/faculty/offutt/research/papers/SLR-CostReductionMutation.pdf)</sup> |
| 核心假设 | 胜任程序员假设：完成的程序是正确的，或者与正确程序之间仅存在“简单”的差异。<sup>[5](https://repository.gatech.edu/server/api/core/bitstreams/af774216-86df-4abd-912c-409d36d99717/content)</sup> |
| 杀死条件 | 强杀死要求可达性、感染以及变更传播至可观察输出。<sup>[6](https://plrg.korea.ac.kr/courses/aaa705/2024_1/slides/lec7-handout.pdf)</sup> |
| 典型开销 | PIT 为 JFreeChart 1.0.19（47 KLOC，1,320 个测试）在 109 分钟内生成了 256K 个变异体，得分为 19%。<sup>[2](https://softengbook.org/articles/mutation-testing)</sup> |
| 标准算子集 | 五个算子（ROR、LCR、AOR、ABS、UOI）通常被视为最低标准。<sup>[1](https://mutationtesting.uni.lu/survey.pdf)</sup> |
| 主要失效模式 | 等价变异体，没有测试能够杀死它们；一般而言检测等价变异体是不可判定的。<sup>[7](https://huang.isis.vanderbilt.edu/cs4278-sp26/readings/mutation-testing.pdf)</sup> |
| 知名工具 | PIT 与 Major（Java）、Stryker（JavaScript/.NET 等）、Mutatest（Python）、Proteum（C）。<sup>[6](https://plrg.korea.ac.kr/courses/aaa705/2024_1/slides/lec7-handout.pdf)</sup> |

## 工作原理

变异分析自动对程序语法进行变异，以产生语义变体，即变异体。<sup>[1](https://mutationtesting.uni.lu/survey.pdf)</sup> 每个变异体源自一次变异算子的单次应用，变异算子是一种简单的语法或语义变换，例如将一个关系算子替换为另一个。<sup>[5](https://repository.gatech.edu/server/api/core/bitstreams/af774216-86df-4abd-912c-409d36d99717/content)</sup> 当某个测试能够区分变异体与原始程序的行为时，该变异体即被杀死；否则它便是存活的。<sup>[1](https://mutationtesting.uni.lu/survey.pdf)</sup>

杀死一个变异体需要三个条件：测试必须到达被变异的代码（可达性），变更必须感染程序状态（感染），且被感染的状态必须传播为可观察的失败（传播）。弱杀死仅要求可达性和感染，即在被变异语句之后立即比较状态，而非在结束时比较。<sup>[6](https://plrg.korea.ac.kr/courses/aaa705/2024_1/slides/lec7-handout.pdf)</sup>

该方法基于两个假设。胜任程序员假设认为，开发者产出的程序近乎正确，只需少量语法改动；一项对代码库缺陷的研究发现，修复通常只需三到四个词元。<sup>[1](https://mutationtesting.uni.lu/survey.pdf)</sup> 耦合效应由 A. Jefferson Offutt 于 1992 年在《ACM 软件工程与方法学汇刊》上形式化提出，该效应认为复杂变异体与简单变异体相耦合，因此能检测所有简单变异体的测试集也能检测很大比例的复杂变异体；针对二阶和三阶变异体的实验发现，对一阶变异体充分的测试杀死了其中超过 99%。<sup>[7](https://huang.isis.vanderbilt.edu/cs4278-sp26/readings/mutation-testing.pdf)</sup>

## 实施方法

按照最初的构想，变异测试分四步进行：执行原始程序、生成变异体、执行变异体、分析变异体；随后计算得分。<sup>[4](https://www.albany.edu/faculty/offutt/research/papers/SLR-CostReductionMutation.pdf)</sup> 第 3 步高度自动化，而第 4 步——对存活变异体的人工分析——通常是手工完成的，并被一些人视为实际采用的主要障碍。<sup>[4](https://www.albany.edu/faculty/offutt/research/papers/SLR-CostReductionMutation.pdf)</sup>

算子系统地替换运算符和值：算术算子变异将 \((a + b)\) 变为 \((a * b)\)、\((a - b)\) 和 \((a/b)\)；<sup>[4](https://www.albany.edu/faculty/offutt/research/papers/SLR-CostReductionMutation.pdf)</sup> 商业工具还增加了比较边界、条件取反、返回值以及方法调用删除等变异器。<sup>[8](https://leanpub.com/mutationtesting)</sup> 变异体数量增长迅速；即便是小小的 Min 函数也会生成 44 个变异体。<sup>[9](https://people.cs.gmu.edu/~tr-admin/papers/ISSE-TR-94-00.pdf)</sup> 存活变异体会被分诊：有些揭示了缺失的测试，有些是等价的，还有些是其他变异体的重复。<sup>[1](https://mutationtesting.uni.lu/survey.pdf)</sup>

## 起源

变异测试由 R.G. Hamlet 在《借助编译器测试程序》一文中提出，该文于 1977 年发表在《IEEE 软件工程汇刊》上。<sup>[10](https://doi.org/10.1109/tse.1977.231145)</sup> 其主要概念在 1978 年的“Hints”论文中被形式化，该文描述了在耶鲁大学和[佐治亚理工学院](https://www.edgepedia.org/zh/georgia-tech)系统中实现的程序变异方法，是被引用为开创性文献最多的一篇。<sup>[11](https://stairs.ics.uci.edu/papers/2017/mutation_tool.pdf)</sup><sup> • </sup><sup>[12](https://www.inf.pucrs.br/~zorzo/cs/demillo-mutants.pdf)</sup> 一份由 Allen T. Acree 及其同事于 1979 年撰写的相关技术报告，与 Budd、Acree 和 Hanks 的学位论文一道巩固了该理论。<sup>[13](https://doi.org/10.21236/ada076575)</sup> 一个重要的早期实现来自 Timothy A. Budd 1980 年关于程序测试数据变异分析的博士论文，其继承了 1977–1978 年文献中描述的系统。<sup>[14](https://gwern.net/doc/cs/algorithm/1980-budd.pdf)</sup> 早期系统包括 PIMS（Fortran）以及 CPMS 和 EXPER（Cobol）。<sup>[15](https://www.albany.edu/faculty/offutt/research/papers/mutationCarol-IST2009.pdf)</sup>

## 变体

一阶变异体携带单个变更；二阶变异体携带两个变更，以此类推。高阶变异体数量远为庞大，一个 28 行的 Fortran 程序就有 528,906 个二阶变异体，因此实践中倾向于一阶变异，依赖耦合效应。<sup>[16](https://www.cs.purdue.edu/homes/apm/foundationsBook/samples/mutation-chapter-sample.pdf)</sup> 在 Judy 工具中实现的二阶策略仍将生成的变异体减少了 50% 或更多。<sup>[17](https://psycnet.apa.org/doi/10.1109/TSE.2013.44)</sup>

降低成本的技术分为“做得更快”“做得更聪明”和“做得更少”三类。<sup>[11](https://stairs.ics.uci.edu/papers/2017/mutation_tool.pdf)</sup> 选择性变异由 A. Jefferson Offutt 及其同事于 1996 年在《ACM 软件工程与方法学汇刊》上提出，仅使用 K. N. King 和 A. Jefferson Offutt 于 1991 年构建的 Fortran 系统 Mothra 的 22 个算子中的 5 个（ABS、AOR、LCR、ROR、UOI），在成本降至原来的四分之一或更低的情况下，提供了与非选择性变异几乎相同的覆盖。<sup>[9](https://people.cs.gmu.edu/~tr-admin/papers/ISSE-TR-94-00.pdf)</sup> 变异体采样使用随机子集，仅用 10% 的变异体就能以 99% 的准确率预测最终得分。<sup>[11](https://stairs.ics.uci.edu/papers/2017/mutation_tool.pdf)</sup> 元变异体（变异体模式）技术将所有变异体嵌入一个参数化程序中，节省了编译时间。<sup>[4](https://www.albany.edu/faculty/offutt/research/papers/SLR-CostReductionMutation.pdf)</sup>

## 应用

工具与语言相关：Mothra（Fortran）、Proteum、MiLU 和 MUSIC（C/C++）、muJava、Major、Javalanche 和 PIT（Java）、Stryker（JavaScript）以及 Mutatest（Python）。<sup>[6](https://plrg.korea.ac.kr/courses/aaa705/2024_1/slides/lec7-handout.pdf)</sup> 工具在语言、编译阶段和算子集上各不相同，对测试套件的杀死情况可能意见不一，因此变异得分在不同工具之间未必一致。<sup>[11](https://stairs.ics.uci.edu/papers/2017/mutation_tool.pdf)</sup>

PIT 直接对编译后的字节码进行变异，并且只运行覆盖变异体代码的测试。<sup>[2](https://softengbook.org/articles/mutation-testing)</sup> Stryker 仅对源代码进行变异以避免误报，Stryker.NET 集成到 Azure Pipelines 或 [GitHub Actions](https://www.edgepedia.org/zh/github-actions) 中并带有质量阈值。<sup>[18](https://learn.microsoft.com/en-us/dotnet/core/testing/mutation-testing)</sup> 厂商建议不要追求 100% 的得分，而应关注高风险或业务关键领域。<sup>[18](https://learn.microsoft.com/en-us/dotnet/core/testing/mutation-testing)</sup> 据报道，谷歌和 Meta 已有工业界应用。<sup>[19](https://www.franktip.org/pubs/issta2026.pdf)</sup>

机器学习如今同时影响着变异体生成与分诊。FaRM 由 Thierry Titcheu Chekam 及其同事于 2019 年在《实证软件工程》上提出，利用变异体类型、控制流位置和代码复杂度，将变异体静态分类为可能可杀死、等价或能揭示故障。<sup>[20](https://doi.org/10.1007/s10664-019-09778-7)</sup> 基于神经网络和语言模型的变异体生成器包括 Michele Tufano 及其同事于 2020 年提出的 DeepMutation，<sup>[21](https://doi.org/10.48550/arxiv.2002.04760)</sup> 以及 Renzo Degiovanni 和 Mike Papadakis 于 2022 年提出的 μBert，后者使用预训练语言模型。<sup>[22](https://doi.org/10.48550/arxiv.2203.03289)</sup> LLMutantKiller 生成能杀死存活 StrykerJS 变异体的测试；在来自 13 个 [JavaScript](https://www.edgepedia.org/zh/javascript)/[TypeScript](https://www.edgepedia.org/zh/typescript) 应用的 915 个存活变异体样本上（815 个引发行为变化，100 个等价），LLM 生成的测试杀死了多达 777 个非等价存活变异体，即 95.3%。<sup>[19](https://www.franktip.org/pubs/issta2026.pdf)</sup>

## 局限与替代方案

等价变异体的行为与原始程序完全相同，例如死代码中的变异或删除日志行的变异，没有测试能够杀死它们。<sup>[2](https://softengbook.org/articles/mutation-testing)</sup> 自动检测所有等价变异体是不可能的，因为程序等价性是不可判定的。<sup>[7](https://huang.isis.vanderbilt.edu/cs4278-sp26/readings/mutation-testing.pdf)</sup> 估计的等价变异体比例因研究而异：一项调查中为 10% 到 40%，<sup>[7](https://huang.isis.vanderbilt.edu/cs4278-sp26/readings/mutation-testing.pdf)</sup> 而在对七个 Java 程序的人工抽样中，未被检测到的变异体约有 45% 是等价的，每次分类约需 15 分钟。<sup>[23](https://www.st.cs.uni-saarland.de/publications/files/schuler-stvrbis-2013.pdf)</sup> 平凡编译器等价仅将编译后目标代码与原始程序的目标代码相同的变异体声明为等价，据一项报告称可识别至少 30% 的等价变异体，<sup>[1](https://mutationtesting.uni.lu/survey.pdf)</sup> 但在一项大规模研究中识别出的等价变异体仅占全部变异体的 7%（另有 21% 为重复变异体），这一差异尚未解决。<sup>[6](https://plrg.korea.ac.kr/courses/aaa705/2024_1/slides/lec7-handout.pdf)</sup> 此外还存在一种权衡：窄变异设定了更强的测试目标，但会产生更多等价变异体；宽变异产生的目标更少但更弱。<sup>[24](https://par.nsf.gov/servlets/purl/10679244)</sup>

相对于覆盖率，变异测试已被实证表明强于控制流和数据流测试准则。<sup>[4](https://www.albany.edu/faculty/offutt/research/papers/SLR-CostReductionMutation.pdf)</sup> 一项针对五个开源应用（321,000 行代码）中 357 个真实故障的研究发现，变异体检测与真实故障检测之间存在统计显著的相关性，且独立于覆盖率，平均每个真实故障耦合 2 个变异体。<sup>[25](https://homes.cs.washington.edu/%7Emernst/pubs/mutation-effectiveness-fse2014.pdf)</sup> 然而，后来一项针对 Defects4J 和 CoreBench 的研究发现，一旦控制测试套件规模，变异得分与真实故障检测之间的所有相关性都很弱，不过按变异得分排名靠前的测试套件仍比同等规模的随机套件检测到显著更多的故障。<sup>[26](https://dl.acm.org/doi/10.1145/3180155.3180183)</sup>

## References

1. [Mutation Testing Advances: An Analysis and Survey (Papadakis et al.)](https://mutationtesting.uni.lu/survey.pdf)
2. [Mutation Testing: A Practical Overview, Software Engineering: A Modern Approach (Marco Tulio Valente)](https://softengbook.org/articles/mutation-testing)
3. [Introduction to mutation testing | Stryker Mutator blog (Simon de Lang, 2017)](https://stryker-mutator.io/blog/introduction-to-mutation-testing/)
4. [A Systematic Literature Review of Techniques and Metrics to Reduce the Cost of Mutation Testing](https://www.albany.edu/faculty/offutt/research/papers/SLR-CostReductionMutation.pdf)
5. [Mutation Analysis (DeMillo, Lipton, Sayward, extended paper, Georgia Tech)](https://repository.gatech.edu/server/api/core/bitstreams/af774216-86df-4abd-912c-409d36d99717/content)
6. [Lecture 7 – Mutation Testing (AAA705, Korea University, March 27, 2024)](https://plrg.korea.ac.kr/courses/aaa705/2024_1/slides/lec7-handout.pdf)
7. [An Analysis and Survey of the Development of Mutation Testing (Jia & Harman, IEEE TSE)](https://huang.isis.vanderbilt.edu/cs4278-sp26/readings/mutation-testing.pdf)
8. [Mutation Testing (Filip van Laenen, Leanpub)](https://leanpub.com/mutationtesting)
9. [An Experimental Determination of Sufficient Mutation Operators (Offutt, Lee, Rothermel, Untch, Zapf, 1996)](https://people.cs.gmu.edu/~tr-admin/papers/ISSE-TR-94-00.pdf)
10. [R.G. Hamlet (1977). Testing Programs with the Aid of a Compiler. IEEE Transactions on Software Engineering.](https://doi.org/10.1109/tse.1977.231145)
11. [Does choice of mutation tool matter? (Gopinath et al., Empirical Software Engineering)](https://stairs.ics.uci.edu/papers/2017/mutation_tool.pdf)
12. [Hints on Test Data Selection: Help for the Practicing Programmer (DeMillo, Lipton, Sayward, IEEE Computer, April 1978)](https://www.inf.pucrs.br/~zorzo/cs/demillo-mutants.pdf)
13. [Allen T. Acree and colleagues (1979). Mutation Analysis.. .](https://doi.org/10.21236/ada076575)
14. [Mutation Analysis of Program Test Data (Timothy A. Budd, PhD thesis, Yale University, 1980)](https://gwern.net/doc/cs/algorithm/1980-budd.pdf)
15. [A Mutation Carol: Past, Present and Future (Offutt, Information and Software Technology, 2009)](https://www.albany.edu/faculty/offutt/research/papers/mutationCarol-IST2009.pdf)
16. [Foundations of Software Testing, Chapter 16: Test Adequacy, Program Mutation (Aditya P. Mathur)](https://www.cs.purdue.edu/homes/apm/foundationsBook/samples/mutation-chapter-sample.pdf)
17. [Empirical Evaluation of Second Order Mutation Strategies (Madeyski et al., IEEE TSE 2013)](https://psycnet.apa.org/doi/10.1109/TSE.2013.44)
18. [Mutation testing - .NET | Microsoft Learn (Stryker.NET)](https://learn.microsoft.com/en-us/dotnet/core/testing/mutation-testing)
19. [LLMutantKiller: Using Large Language Models to Generate Tests that Kill Mutants (ISSTA 2026)](https://www.franktip.org/pubs/issta2026.pdf)
20. [Thierry Titcheu Chekam and colleagues (2019). Selecting fault revealing mutants. Empirical Software Engineering.](https://doi.org/10.1007/s10664-019-09778-7)
21. [Tufano, Michele and colleagues (2020). DeepMutation: A Neural Mutation Tool. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2002.04760)
22. [Degiovanni, Renzo, Papadakis, Mike (2022). $μ$BERT: Mutation Testing using Pre-Trained Language Models. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2203.03289)
23. [Covering and Uncovering Equivalent Mutants (Schuler and Zeller)](https://www.st.cs.uni-saarland.de/publications/files/schuler-stvrbis-2013.pdf)
24. [Equivalent Mutants in the Wild: Identifying and Efficiently Suppressing Equivalent Mutants for Java Programs](https://par.nsf.gov/servlets/purl/10679244)
25. [Are Mutants a Valid Substitute for Real Faults in Software Testing? (Just, Jalali, Inozemtseva, Ernst, Holmes, Fraser, FSE 2014)](https://homes.cs.washington.edu/%7Emernst/pubs/mutation-effectiveness-fse2014.pdf)
26. [Are mutation scores correlated with real fault detection?: a large scale empirical study on the relationship between mutants and real faults (ICSE 2018)](https://dl.acm.org/doi/10.1145/3180155.3180183)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software testing and quality

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/mutation-testing

English original: https://www.edgepedia.org/mutation-testing
