Automatic Generation of Test Cases based on Bug Reports: a Feasibility Study with Large Language Models

Laura Plein, Wendkûuni C. Ouédraogo, Jacques Klein, Tegawendé F. BissyandéarXiv 2023

TL;DR

This paper explores whether Large Language Models (LLMs) can bridge the gap between informal, natural-language bug reports and executable test cases. The researchers found that off-the-shelf models like ChatGPT can generate executable test cases for up to 50% of reported bugs, providing a vital missing link for automated program repair and fault localization.

What Problem Does This Paper Address?

In the world of software engineering, Automated Program Repair (APR) is a "holy grail" technology that aims to fix bugs without human intervention. However, APR tools have a major dependency: they require high-quality, executable test cases to identify where the code is broken (fault localization) and to verify that a proposed fix actually works (patch validation).

The reality is that when a bug is found in the wild, developers don't get a test case; they get a bug report. These reports are often messy, written in natural language, and vary wildly in quality. Manually translating these reports into code is time-consuming. This paper investigates whether LLMs can automate this translation, turning a user's prose into a "bug-triggering" test case that an APR pipeline can actually use.

Figure 6: Typical generate-and-validate APR pipeline (e.g., TBar [16])

Methods

The authors developed a pipeline that takes a raw bug report and feeds it into an LLM with a simple prompt: "write a Java test case for the following bug report: ...".

Figure 1: Pipeline for LLM-based generation of test cases from bug reports

They tested two main approaches:

  1. LLM-as-a-Service: Using ChatGPT (GPT-3.5) as a zero-shot baseline. They tested both single-generation attempts and a "best-of-five" approach to account for the model's inherent randomness.
  2. Fine-tuned Model: They fine-tuned CodeGPT (a version of GPT-2 adapted for Java) specifically on pairs of bug reports and failing test cases to see if specialized training could outperform a general-purpose giant.

The generated tests were evaluated on three levels:

  • Executability: Does the code compile and run?
  • Validity: Does it actually fail on the buggy version of the software?
  • Relevance: Does it fail on the buggy version and pass on the fixed version?

Datasets

  • Defects4J: The primary benchmark, consisting of 972 pairs of bug reports and failing test cases across several popular Java projects (like Jsoup, Mockito, and Commons-Lang).
  • New Bug Dataset: To ensure the LLMs weren't just "memorizing" known bugs from their training data (data leakage), the authors collected 38 new bug reports from GitHub/StackOverflow created after October 2021 (ChatGPT's knowledge cutoff).

Key Insights and Ideas

  • The Power of Code Snippets: The most significant predictor of success wasn't the length of the bug report, but whether it contained code snippets. 90% of the "relevant" test cases were generated from reports that included at least some code.
  • Complexity vs. Conciseness: Interestingly, LLM-generated test cases tend to have higher cyclomatic complexity than those written by human developers. While humans aim for the minimal reproduction case, LLMs often try to mirror the detailed execution scenarios described in the report.
Figure 3: Cyclomatic complexity of ground-truth bug- triggering test cases (Original TC) vs LLM-generated valid test cases (Generated TC)
  • Beyond Human Performance: In a surprising twist, the LLM-generated tests sometimes outperformed human-written ones for fault localization. Because the LLM tests were more complex, they occasionally provided a better "signal" for tools like GZoltar to pinpoint the exact buggy line of code.
  • Fine-tuning vs. Scale: While fine-tuning CodeGPT helped in "single-shot" scenarios, the sheer scale and reasoning capability of ChatGPT (especially with multiple attempts) eventually won out, suggesting that general reasoning is as important as specialized syntax knowledge for this task.

Key Results

  • ChatGPT Success: With five attempts, ChatGPT generated executable test cases for 50% of the bugs and valid (bug-triggering) tests for 30%.
  • Generalization: On the "new bug" dataset, ChatGPT maintained a 55% executability rate, proving the approach works on previously unseen, real-world issues.
  • APR Utility: The generated tests successfully discarded 21 out of 29 "plausible but incorrect" patches generated by an APR tool, showing they are highly effective filters for patch validation.

Limitations and Future Work

  • Language Constraint: The study focused exclusively on Java. Future work is needed to see if these results hold for languages with different ecosystems (like Python or C++).
  • The "Almost Executable" Gap: Many failed tests were only off by a single line (e.g., a missing import or a duplicate function name). The authors suggest that simple automated post-processing or better prompt engineering could significantly boost the success rate.
  • Prompt Sensitivity: The study used a very basic prompt. Exploring "Chain-of-Thought" prompting or providing more project context (like class signatures) could further improve relevance.

Summary updated

BibTeX
@misc{plein2023automatic,
  author = {Laura Plein and Wendkûuni C. Ouédraogo and Jacques Klein and Tegawendé F. Bissyandé},
  title = {{Automatic Generation of Test Cases based on Bug Reports: a Feasibility Study with Large Language Models}},
  year = {2023},
  eprint = {2310.06320},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2310.06320},
}
Figure