NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Efficient Mutation Testing via Pre-Trained Language Models

Ahmed Khanfir, Renzo Degiovanni, Mike Papadakis, Yves Le TraonarXiv 2023 · DOI

Introduction

Mutation testing is a critical technique for evaluating the effectiveness of software test suites. By intentionally injecting small bugs, or "mutants," into a program, developers can determine if their tests are capable of detecting real-world errors. However, traditional mutation testing often relies on rigid, hand-crafted syntactic rules (e.g., changing + to -), which frequently result in "unnatural" mutants that do not resemble actual developer mistakes. This paper presents μBERT, a novel mutation testing tool that leverages the CodeBERT language model to generate contextually relevant and "natural" faults.

Methodology: The μBERT Workflow

μBERT moves beyond simple syntactic swaps by using Masked Language Modeling (MLM) to predict plausible code replacements based on the surrounding context. The workflow is divided into five primary stages:

  1. AST Selection: The tool parses the Abstract Syntax Tree (AST) to target business logic, avoiding boilerplate code.
  2. Token Masking: Selected tokens are replaced with a <mask\> placeholder.
  3. MLM Prediction: CodeBERT predicts the top 5 most likely replacements for the mask.
  4. Condition Seeding: To capture complex logic errors, μBERT can inject new boolean conditions (e.g., if (a || b > 0)) and use the model to complete them.
  5. Injection & Compilation: Mutants are filtered to ensure they compile and are not functionally identical to the original code.
The μBERT workflow: from source code selection to the injection of context-aware faults.

Key Insights: Contextual and Additive Mutations

The primary advantage of μBERT is its "understanding" of code semantics. Traditional tools treat every instance of an operator identically, whereas μBERT tailors its mutations to the specific variables and logic present. For example, it might suggest changing a numerical constant to 0 if it acts as a counter, or replacing a method call with a semantically related one (e.g., add to push).

Examples of AST nodes and the corresponding natural mutants generated by μBERT's masking process.

Furthermore, the introduction of Condition Seeding allows μBERT to generate "second-order" mutants. By adding logic rather than just swapping it, the tool can simulate subtle boundary-condition errors that are often missed by rule-based systems.

Evaluation and Results

The researchers evaluated μBERT using the Defects4J benchmark, analyzing 689 faulty program versions from large-scale Java projects. The results demonstrate significant improvements over traditional methods:

  • Higher Fault Revelation: μBERT mutants successfully guided test suites to detect 84% of real-world faults on average.
  • Outperforming Industry Standards: μBERT showed up to a 17% higher fault detection potential compared to PiTest, the current state-of-the-art tool.
  • Unique Faults: μBERT identified 47 unique bugs that PiTest missed entirely. While PiTest is efficient at catching simple arithmetic slips, μBERT excels at uncovering complex logic-heavy bugs.
  • Efficiency: Even with limited analysis effort, μBERT consistently finds more bugs earlier in the testing process than conventional tools.

Conclusion and Future Work

μBERT demonstrates that Large Language Models can significantly enhance mutation testing by generating faults that are both natural and challenging. While the current implementation is focused on Java and carries a higher computational cost than regex-based tools, the quality of the mutants justifies the trade-off. Future research will explore the application of even larger models, such as GPT-4, and expand the tool's support to other programming languages like Python and C++.

Summary updated

BibTeX
@misc{khanfir2023efficient,
  author = {Ahmed Khanfir and Renzo Degiovanni and Mike Papadakis and Yves Le Traon},
  title = {{Efficient Mutation Testing via Pre-Trained Language Models}},
  year = {2023},
  eprint = {2301.03543},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2301.03543},
}
Figure