SWT-Bench: Testing and Validating Real-World Bug-Fixes with Code Agents

Niels Mündler, Mark Niklas Müller, Jingxuan He, Martin Vechev · DOI

TL;DR

This paper introduces SWT-BENCH, a new benchmark for automatically generating unit tests that reproduce real-world software bugs. They demonstrate that LLM-based "Code Agents," originally designed for code repair, are surprisingly effective at test generation and can even improve the precision of bug-fixing systems.

What Problem Does This Paper Address?

Developing and maintaining high-quality software relies heavily on rigorous testing. While automated code generation using Large Language Models (LLMs) is a rapidly advancing field, the automated generation of tests to reproduce specific bugs has lagged behind. This is a critical gap because manually creating tests to replicate reported issues is time-consuming and often disliked by developers. Furthermore, effective tests are crucial for validating automatically generated code fixes. The authors aim to bridge this gap by investigating how LLM-based Code Agents can generate tests that accurately reflect user-reported issues.

Methods

The core of this work is the introduction of SWT-BENCH, a novel benchmark specifically designed for test generation aimed at issue reproduction. This benchmark is built upon the existing SWE-BENCH dataset, incorporating real-world GitHub issues, corresponding bug-fix patches, and "golden" reference tests.

A key methodological contribution is the adaptation of Code Agents for test generation. These agents, which can interact with their environment (like a code editor and terminal), are repurposed from their original code repair tasks. The authors modify their instructions to focus on creating tests rather than fixes. They also propose a custom diff format that is more robust and easier for LLMs to generate compared to the standard unified diff format, particularly for adding test functions.

For evaluation, they use two primary metrics:

  • Success Rate (S): The percentage of issues for which generated tests correctly fail on the original code and pass after the bug fix.
  • Change Coverage (C): The proportion of lines modified by the bug-fix patch that are executed by the generated tests.

Datasets

The primary dataset introduced is SWT-BENCH. It is derived from the popular SWE-BENCH dataset, which itself is based on real-world GitHub issues and codebases. SWT-BENCH contains over 1900 task instances, each comprising:

  • A GitHub issue description.
  • A golden code patch that fixes the issue.
  • A set of golden reference tests.

A smaller subset, SWT-BENCH-LITE, is also used for more cost-effective evaluation.

Key Insights and Ideas

  • Code Agents Excel at Test Generation: Surprisingly, Code Agents, even when adapted from code repair, outperform specialized test generation methods. This suggests that the interactive and contextual reasoning capabilities of agents are highly beneficial for this task.
  • Test Generation is Distinct from Code Repair: While related, the ability to generate a test that reproduces a bug is not perfectly correlated with the ability to fix it. This implies different underlying challenges and skill sets are required.
  • Generated Tests as a Quality Filter: The generated tests can act as a powerful filter for proposed code fixes. By ensuring that a generated test fails before a fix and passes after, the precision of automated bug-fixing systems can be significantly improved (doubled in their experiments with SWE-AGENT).
  • Custom Diff Format Matters: A more LLM-friendly custom diff format significantly improves the ability of models to generate correct and applicable patches (e.g., boosting well-formedness from 48.6% to 89.5% for ZEROSHOTPLUS).
  • Issue Description Length is Informative: Longer issue descriptions generally lead to higher success rates for test generation, likely due to more detailed context, though extremely long descriptions can introduce distractors.

Key Results

  • Code Agents Outperform: SWE-AGENT, a Code Agent, achieved a success rate (S) of 15.9%, which was further improved to 18.5% when instructed to verify its generated tests (SWE-AGENT+). This surpassed other methods like LIBRO (14.1%) and AIDER (12.7%).
  • Improved Fix Precision: Using generated tests to filter code fixes doubled the precision of SWE-AGENT, from an unspecified baseline to 47.8%.
  • High Patch Applicability: Methods using the custom diff format (ZEROSHOTPLUS) achieved a high rate of well-formed patches (89.5%).
  • Coverage Correlates with Quality: Successful test generation instances showed significantly higher change coverage (ACS) compared to unsuccessful ones (AC¬S), indicating coverage is a good indicator of test quality.

Limitations and Future Work

  • Python-Specific: The current benchmark and evaluation are limited to Python, which may not generalize to other programming languages.
  • Public Repository Bias: The dataset is based on popular GitHub repositories, which might not represent the full spectrum of software development practices, and a private holdout test set is not feasible.
  • Bug Reproduction Focus: The dataset primarily focuses on bug reproduction and issues addressable by adding test cases, not necessarily on detecting edge cases or measuring global coverage increases.
  • Data Contamination Risk: Issues in SWT-BENCH may have been part of the LLMs' pre-training data, posing a risk of data contamination. The authors suggest creating a rolling version of the benchmark to mitigate this.
  • Future Directions: The authors suggest addressing limitations by expanding to other languages, creating rolling benchmarks, and improving LLM performance by tackling common errors like generating passing but irrelevant tests or getting stuck in loops.

Summary updated

BibTeX
@inproceedings{mundler2024swt,
  author = {Niels Mündler and Mark Niklas Müller and Jingxuan He and Martin Vechev},
  title = {{SWT-Bench: Testing and Validating Real-World Bug-Fixes with Code Agents}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=9Y8zUO11EQ},
}
Figure