Introduction
Automated test generation using Large Language Models (LLMs) has become a cornerstone of modern software engineering, promising to reduce developer workload and improve code reliability. The standard workflow involves prompting an LLM with source code and requesting a comprehensive test suite. However, this practice rests on a critical, often untested assumption: that the source code provided in the prompt is correct. This paper investigates the relationship between source code correctness and the effectiveness of LLM-generated tests, revealing a significant vulnerability in current testing practices.
Experimental Methodology
The researchers conducted an extensive empirical study involving 11 prominent LLMs, including five open-source models (e.g., Llama-3, DeepSeek-Coder) and six closed-source models (e.g., GPT-4, Claude-3). To isolate the impact of code correctness, the study employed five distinct prompt templates:
- Task description only (P_T)
- Task description + Correct Code (P_T_CC)
- Task description + Incorrect Code (P_T_IC)
- Correct Code only (P_CC)
- Incorrect Code only (P_IC)
Performance was measured across three dimensions: Accuracy (tests passing against a ground-truth implementation), Coverage (percentage of code lines executed), and Bug Detection (ability to identify known errors). The study utilized standard benchmarks (HumanEval, MBPP, APPS) alongside complex, real-world datasets (BugsInPy, SWE-Bench).
The "Baking In" Effect: How Incorrect Code Compromises Testing
A primary finding of the study is that LLMs are highly susceptible to "baking in" errors. When presented with incorrect code, LLMs tend to treat it as the ground truth, generating test cases that validate the faulty logic rather than exposing it. This behavior effectively blinds the generated test suite to the very bugs it is intended to find.
As shown in the correctness distributions, there is a marked decline in performance when moving from correct code prompts (P_T_CC) to incorrect code prompts (P_T_IC). This suggests that LLMs lack the independent reasoning required to verify code logic against a task's requirements when a concrete (albeit flawed) implementation is present.
Quantitative Performance Analysis
The impact of incorrect code is quantifiable and severe across all metrics. On average, prompting with a task description and correct code yielded a test accuracy of 73.9%. Replacing the correct code with an incorrect version caused accuracy to plummet to 47.1%—a 57% relative decrease.
Beyond accuracy, line coverage dropped from 96.8% to 86.5% (a 12% decrease), and bug detection rates fell by 24%. The degradation was even more pronounced in real-world scenarios; on the BugsInPy and SWE-Bench datasets, tests generated from incorrect code saw a 47% lower bug detection rate compared to those generated from correct code.
The Role of Task Descriptions as a Corrective Anchor
One of the most actionable insights from the study is the "anchoring" effect of natural language task descriptions. Providing a clear description of what the code should do significantly mitigates the negative impact of buggy source code. Adding a task description to a correct code prompt improved accuracy by 18% and bug detection by 34% compared to providing the code alone. This suggests that task descriptions provide a necessary frame of reference that allows the LLM to better understand the intended logic, even if the provided implementation is flawed.
Real-World Challenges and Future Directions
While LLMs show promise in synthetic benchmarks, their performance degrades significantly in repository-level tasks. In complex environments like SWE-Bench, the massive context windows required (often exceeding 13,000 tokens) appear to impair the models' reasoning abilities. Furthermore, the study highlights that LLMs are currently better suited for regression testing—protecting mature, correct code—than for early-stage development where code is likely to be buggy.
Conclusion
This research demonstrates that the correctness of source code in a prompt is a dominant factor in the quality of LLM-generated tests. To avoid "baking in" bugs, developers should prioritize including detailed natural language task descriptions in their prompts. Future work must focus on improving LLM resilience to incorrect code and enhancing their ability to handle the long-context requirements of real-world software repositories.
Summary updated