Code evaluation · Research feature

On the risk of coding before testing: An empirical study on LLM-based test generation workflow

A study reveals that tests created after faulty code are often aligned with errors, reducing their ability to detect defects.

Adapted from research by Michael Konstantinou, Florian Tambon, Mike Papadakis.

arXiv preprint revision · 2026-07-06 · Attribution & reuse
In this article
Editorial illustration of linked workflow.

Editor’s introduction

The study focuses on large language models generating both code and tests in sequence. Researchers measured whether errors in generated code were replicated in the associated test cases, particularly when tests were produced after code implementation. They found that when tests follow code generation, the tests frequently pass even when the code is incorrect, creating a false sense of reliability. The study shows that test generation after code produces 14% fault detection compared to 25% when tests are generated independently. The findings highlight a fundamental issue in current LLM-based development pipelines: generated tests may not serve as independent validators of correctness.

Introduction

Large Language Models (LLMs) are increasingly integrated into software engineering workflows, where they are used not only to generate production code but also to synthesize accompanying test suites. This dual capability has fueled optimistic visions of highly automated development pipelines, in which both implementation and validation are delegated to the same generative system. In particular, recent toolchains promote test-first or co-evolutionary workflows, where LLMs generate candidate tests and subsequently produce code that satisfies them, potentially accelerating development while reducing human effort. However, this paradigm implicitly assumes that LLM-generated tests provide an independent and reliable oracle for assessing correctness. In traditional software testing, effectiveness critically depends on the independence between the system under test and the test oracle. When both artifacts are produced by the same underlying model, this assumption may no longer hold.

In this paper, we investigate this fundamental assumption by examining whether the generation of faulty code by an LLM biases the generation of tests. When tasked with producing both artifacts for the same problem, an LLM is likely to rely on similar internal representations, assumptions, and reasoning trajectories. This shared generative process increases the likelihood that errors are not independent but systematically replicated, leading to tests that encode and validate the same incorrect behavior exhibited by the generated code.

Moreover, this issue is exacerbated by the autoregressive nature of LLMs. These models generate outputs token by token, conditioning each step on previously generated content. As a result, when an LLM produces code containing errors, this faulty output becomes part of the context that shapes subsequent generations. Consequently, follow-up activities—such as test generation—can be implicitly biased by earlier mistakes, increasing the risk of propagating erroneous assumptions across artifacts.

This implies that errors introduced during code synthesis—such as incorrect interpretations of specifications, boundary conditions, or edge cases—are not independent from subsequent testing activities. Instead, they are often replicated during test generation, including in the construction of test oracles (e.g., assertions). This behavior is particularly critical in agentic workflows, especially in “vibecoding” scenarios where autonomous agents iteratively solve tasks with minimal human intervention. Furthermore, it raises concerns for empirical studies that do not isolate code generation from test generation, as such coupling may systematically bias evaluation results.

We refer to this phenomenon as error propagation, whereby implementation faults are mirrored by corresponding faults in generated tests. In such cases, the tests validate incorrect behavior rather than expose it, creating a false sense of correctness. Importantly, this is not merely a consequence of poor prompt design or training data limitations, but a structural effect of autoregressive generation, where similar probabilistic biases and reasoning patterns are reused across related outputs.

Recent research has shown that incorporating erroneous artifacts, such as faulty code, into prompts can degrade LLM performance. In the context of test oracle generation, studies have demonstrated that the inclusion of incorrect or misleading code during prompting, can negatively affect the correctness of generated assertions and reduce their effectiveness as validation mechanisms. Building on these findings, we argue that the impact of erroneous artifacts extends beyond individual tasks to multi-step workflows, which more closely reflect current practice in modern agentic systems and “vibecoding.” In such settings, intermediate outputs—potentially containing errors—are explicitly reused as part of the context in subsequent steps, forming iterative feedback loops that shape future generations.

Moreover, most contemporary agentic workflows rely on prompt decomposition or planning strategies, where complex tasks are broken down into sequences of sub-tasks processed step by step. While such structured approaches generally improve performance compared to single-shot prompting, they introduce strong interdependencies between steps, since each sub-task conditions on artifacts produced earlier in the workflow. Consequently, the assumption of independence between reasoning steps no longer holds. Errors introduced in earlier stages may bias or constrain subsequent generations, leading to cascading effects across the workflow.

Given that workflow design is a critical factor influencing the performance of LLM-based systems, we conduct an empirical study of agentic code-and-test generation workflows across a diverse set of programming tasks. Specifically, we analyze the consistency between generated implementations and their corresponding test assertions, with particular emphasis on scenarios in which both artifacts are incorrect yet mutually consistent. Such cases are particularly problematic, as they may falsely indicate correctness despite underlying faults.

Methodology

Given a programming task, a developer can leverage an LLM or an LLM-based coding assistant to generate an implementation from a natural-language specification. Subsequently, either the developer or an automated tool may prompt the same model to generate unit tests with the goal of validating the implementation and uncovering potential faults.

Numerous approaches have been proposed, both in academia and industry, for generating tests using LLMs. Despite their differences, these approaches typically rely, either explicitly or implicitly, on access to the implementation under test. The generated code is therefore often used as the primary source of information for guiding test generation.

In this work, we investigate whether access to the implementation is necessary for effective test generation. Specifically, we compare existing code-aware test generation workflows against a simple baseline that relies exclusively on the original task description, without any access to the generated implementation. By doing so, we assess the extent to which the natural-language specification alone contains sufficient information to guide fault-revealing test generation.

To conduct this study, we perform the following steps. First, for each programming task, we use an LLM to generate one or more candidate implementations. Second, we identify and retain only faulty implementations, thereby constructing a benchmark of incorrect solutions. Finally, we apply a range of test generation workflows to each faulty implementation and evaluate their effectiveness in detecting faults. This experimental design enables a systematic comparison between implementation-aware and specification-driven test generation approaches, providing insights into the relative value of code and task descriptions as sources of testing information.

Data Collection: To construct our dataset of faulty implementations, we employ three well-established programming benchmarks. Each benchmark provides a task description, a reference implementation, and a test suite for validating generated solutions. For every task, we prompt each model to generate an implementation based solely on the task description. To obtain a diverse set of candidate implementations while mitigating sampling bias, we generate ten solutions per task using a temperature of 0.8, which encourages variation in the models’ outputs.

Each generated implementation is executed against the benchmark’s reference test suite. Since our study focuses on the effectiveness of generated tests in detecting behavioral faults, we retain only implementations that produce incorrect outputs for at least one test case. Any implementations that fail due to runtime errors, exceptions, or other execution failures are excluded.

Faults ranking and selection: The previous phase produces a set of candidate faulty implementations for each programming task. However, multiple faulty implementations may be generated for the same task, and some may contain faults that are trivially detectable.

LLM-based test generation: To evaluate the impact of implementation exposure on test generation, we require a test generation workflow that minimizes confounding factors and allows differences in performance to be attributed solely to the test strategy under investigation. We therefore adopt LLM-Plain, a lightweight test generation approach that relies solely on the reasoning capabilities of the underlying LLM.

Test generation strategies

Modern LLM-based test generation tools typically combine multiple prompting strategies, iterative refinement loops, and auxiliary analyses to improve the quality of the generated test suites. As we do not aim to evaluate the tools but rather the impact of their use within different workflows. The selected strategies reflect the current state of practice in LLM-based test generation, as each has been proposed in prior work or adopted by existing test generation tools.

Baseline: As we aim to study reasoning workflow impact on agent’s code-test-generation performance, we consider a simple agentic baseline without any added reasoning capability. We use LLM-Plain, a simple iterative error-refinement loop to ensure test suite are executable. This ensures some agentic capability without including any added reasoning capability and enables us to isolate and evaluate the contribution of each strategy independently.

Chain-of-Thought (CoT): Before generating the final test suite, the LLM is encouraged to explicitly reason about the expected behavior of the implementation and identify relevant scenarios to test. The resulting reasoning is then used to guide the subsequent generation of unit tests. CoT has been successfully incorporated into code generation workflows.

Chain-of-Verification (CoVe): Unlike Chain-of-Thought, which encourages reasoning before generating a response, Chain-of-Verification introduces a self-verification phase after an initial response has been produced. The LLM first generates a candidate test suite and is subsequently prompted with a series of verification questions that encourage it to identify and correct potential mistakes. CoVe and similar designs were applied to improve code and test generation through several studies.

Agentic workflow: Unlike the previous techniques, this workflow mimics an interactive session from the initial code generation request. The model first receives a code generation request and produces the (faulty) implementation. The user then asks the same model to generate unit tests. To achieve this, we preserve the entire conversation history from code generation through test generation within a single session. To ensure that all workflows are evaluated on identical faulty implementations, we control the model’s initial code generation response to exactly match the faulty implementation(s) collected at an earlier stage.

Research protocol

To answer RQ1, we conduct three complementary experiments that evaluate the impact of code exposure on LLM-based test generation. In all experiments, we employ the LLM-Plain workflow described in Section IV-A, ensuring that the evaluated strategy is the only aspect that is controlled in the different configurations.

We first evaluate the direct impact of exposing the implementation under test during test generation. Unlike prior work, which compares prompting strategies using a single interaction with the LLM, we perform this comparison within a realistic LLM-based test generation workflow that reflects the design of modern test generation approaches. We generate test suites using the three input configurations: (1) Task description only, (2) Task description and implementation under test, and (3) Implementation under test only. It is noted that each configuration and task is performed on a fresh trial to avoid having potential bias from previous runs.

To answer RQ2, we investigate whether prompt engineering techniques commonly adopted by recent LLM-based test generation and code generation workflows mitigate the impact of implementation exposure. We compare the task-description-only baseline against three representative strategies: summarization-based test generation, Chain-of-Thought prompting, and Chain-of-Verification.

To answer RQ3, we simulate a typical vibe-coding (agentic) workflow in which a developer first prompts the LLM to generate an implementation from a task description and subsequently requests a unit test suite for the generated code. To faithfully reproduce this interaction, we preserve the conversational history throughout the workflow. Specifically, the model first receives the task description and generates an implementation. Since we are interested on what happens when the model generates faulty code, we only consider these cases, i.e., we discard the cases that the model is generating correct code and keep only those that are faulty. We then prompt the model to generate unit tests.

Empirical study on test generations

Several studies investigated the capability of LLMs to generate tests. Konstantinou et al. investigated the performance of four representative execution-feedback (LLM-based) test generation methods (HITS, SymPrompt, TestSpark and Coverup ) against a simple LLM-plain baseline. They showed that LLM-plain outperforms existing methods when using newer LLMs. Yuan et al. studied unit test generations with ChatGPT, finding that many of the tests lead to compilation errors and therefore proposed integrating compilation feedback to reduce them. Abdullin et al. compared traditional test generation methods such as symbolic execution or SBST against LLM-based methods. They observed that LLM-based method could outperform more traditional ones in terms of mutation score.

All the above studies aim at generating regression tests that assert the observed program behavior and therefore, by construction, cannot detect faults in the target implementations under test. To detect faults, tests need to be equipped with oracles that assert the expected program behavior. To this end, TOGA and TOGLL use Evosuite to generate test prefixes, and leverage LLMs to generate the test oracles. They found that LLMs generate many wrong assertions. Konstantinou et al. showed that the inclusion of the faulty code in the LLM prompt negatively biasses the correctness of the generated assertions making them hard to detect faults. Going a step further, Huang et al. showed that using the faulty code together with the task descriptions (or correct code) improves the fault detection ability of tests/assertions.

Similarly to the above studies, we use LLMs to generate tests and oracles from the task descriptions, without including any faulty code in the candidate prompts to avoid potential bias. Differently though, rather than checking the impact of including a faulty implementation in the prompt, we examine the indirect impact of having a code generation during an earlier step within the agentic workflow.

Conclusion

Large language models are increasingly being integrated into software development workflows, where they are used not only to generate code but also to generate the tests intended to validate that code. While this workflow offers substantial productivity benefits, it raises a fundamental concern: does generating tests after code lead to tests that validate the implementation rather than the intended specification?

In this paper, we conducted an empirical study of the LLM-based code-before-test workflow and investigated its impact on the effectiveness of generated test suites. Our results indicate that tests generated after implementation are often strongly influenced by the code they observe, resulting in a tendency to reinforce implementation assumptions and overlook defects. Consequently, high test pass rates and code coverage do not necessarily imply that the generated implementation conforms to the original specification. Instead, such tests may provide a false sense of correctness by validating the same interpretation adopted during code generation.

The findings highlight a previously underexplored risk of LLM-assisted development: the loss of independence between implementation and verification. This issue becomes particularly problematic when task descriptions are ambiguous or underspecified, as both the generated code and the generated tests may converge to the same incorrect interpretation. In such cases, the testing process fails to serve its primary purpose of identifying behavioral deviations from the intended requirements.

Our study contributes empirical evidence that the ordering of code and test generation matters in LLM-driven software engineering workflows. The results suggest that practitioners should exercise caution when relying exclusively on post-hoc LLM-generated tests as correctness oracles and should prefer workflows that preserve a degree of separation between specification, implementation, and validation.

Future work can explore techniques to mitigate this risk, including specification-first prompting, independent test generation models, adversarial test generation, and human-in-the-loop validation strategies. More broadly, we believe that understanding the interactions between multiple LLM-generated artifacts will be essential for building trustworthy AI-assisted software engineering processes. As LLMs continue to assume greater responsibility in development workflows, ensuring that testing remains an independent and effective mechanism for quality assurance will be critical in realizing their potential.