[论文解读] Efficient Mutation Testing via Pre-Trained Language Models
本文提出 $μ$ Bert,一种基于 CodeBERT(一种预训练代码语言模型)的变异测试技术,通过基于上下文预测对代码中的标记进行掩码和替换,生成自然、类似开发人员的变异体。在 689 个 Defects4J 漏洞上评估表明,$μ$ Bert 比 PiTest 多发现最多 17% 的缺陷,展现出更高的缺陷检测能力与更高的成本效益,同时生成的变异体更具真实性。
Mutation testing is an established fault-based testing technique. It operates by seeding faults into the programs under test and asking developers to write tests that reveal these faults. These tests have the potential to reveal a large number of faults -- those that couple with the seeded ones -- and thus are deemed important. To this end, mutation testing should seed faults that are both "natural" in a sense easily understood by developers and strong (have high chances to reveal faults). To achieve this we propose using pre-trained generative language models (i.e. CodeBERT) that have the ability to produce developer-like code that operates similarly, but not exactly, as the target code. This means that the models have the ability to seed natural faults, thereby offering opportunities to perform mutation testing. We realise this idea by implementing $μ$BERT, a mutation testing technique that performs mutation testing using CodeBert and empirically evaluated it using 689 faulty program versions. Our results show that the fault revelation ability of $μ$BERT is higher than that of a state-of-the-art mutation testing (PiTest), yielding tests that have up to 17% higher fault detection potential than that of PiTest. Moreover, we observe that $μ$BERT can complement PiTest, being able to detect 47 bugs missed by PiTest, while at the same time, PiTest can find 13 bugs missed by $μ$BERT.
研究动机与目标
- 解决传统变异测试的局限性,即由于严格的语法规则而生成不自然或琐碎的变异体。
- 通过生成反映真实编程模式和惯例的变异体,提升缺陷检测的有效性。
- 利用 CodeBERT 等预训练语言模型,生成上下文相关、类人的代码变异体,而无需依赖语法规则或缺陷知识。
- 在真实世界漏洞上评估该方法,以比较其与 PiTest 等先进工具在缺陷检测效果和成本效益方面的表现。
- 研究添加型变异(尤其是条件播种)对缺陷检测能力的影响。
提出的方法
- 利用 CodeBERT 的掩码语言建模(MLM)能力,基于上下文预测为代码中的掩码标记生成合理替换,从而生成上下文恰当的变异体。
- 遍历程序语句中的每个标记,将其掩码,并利用 CodeBERT 的前几项预测结果生成多个候选替换。
- 通过过滤机制剔除无法编译、重复以及语法等价的变异体,以确保生成的变异体有效且多样化。
- 通过扩展控制流条件(例如添加逻辑条件如 `||` 或 `&&`)引入添加型变异,并重新掩码,使 CodeBERT 完成表达式生成。
- 将直接标记替换变异体与条件播种变异体结合,以提升变异体多样性,并覆盖更复杂的缺陷模式。
- 利用生成的变异体指导测试套件生成,测量能杀死这些变异体的测试所发现的真实缺陷数量。

实验结果
研究问题
- RQ1预训练语言模型能否生成比传统语法变异算子更自然、更有效的变异体?
- RQ2在不同配置(DEFAULT、ALL、RV)下,$μ$ Bert 的缺陷检测能力与 PiTest 相比如何?
- RQ3添加型变异(特别是条件播种)在多大程度上能提升缺陷检测能力,超越简单的标记替换?
- RQ4在按生成变异体数量衡量时,$μ$ Bert 相较于 PiTest 的成本效益如何?
- RQ5变异体多样性与可读性对真实世界缺陷检测中测试有效性的实际影响是什么?
主要发现
- 平均而言,$μ$ Bert 在 Defects4J 的 689 个真实漏洞中揭示了 84% 的缺陷,表明其具备强大的缺陷检测能力。
- $μ$ Bert 在所有三种 PiTest 配置(DEFAULT、ALL、RV)中均表现更优,能杀死 $μ$ Bert 变异体的测试套件可多检测出 5.5% 至 33% 的真实缺陷。
- 即使在相同数量的变异体下,$μ$ Bert 所诱导的测试套件仍比 PiTest 多检测出 6% 至 16% 的缺陷。
- 直接使用 CodeBERT 预测与条件播种变异体相结合的方法,比仅使用直接预测多检测出超过 9% 的缺陷。
- $μ$ Bert 检测到 47 个 PiTest 未发现的缺陷,而 PiTest 检测到 13 个 $μ$ Bert 未发现的缺陷,表明二者具有互补优势。
- 该方法成本效益高,无需语法规则或缺陷特定知识,完全依赖于预训练阶段学习到的代码分布。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。