Evaluating and Mitigating the Misguidance Effect of Buggy Code in LLM-Generated Unit Tests
Junda Zhao, Shurui Zhou, Eldan Cohen
University of Toronto
Key points
From the paper's conclusion- We present a new metric to quantitatively measure the 'misguidance effect,' a phenomenon where buggy code steers LLMs toward generating tests that validate its erroneous behavior rather than expose it, and our analysis reveals that prompting LLMs with buggy code has a severe, twofold impact: it significantly increases 'misguided tests' that assert incorrect behavior while simultaneously suppressing the generation of effective, bug-finding tests.
- To counter this, we introduce and validate a specification-based unit test generation paradigm that replaces the code under test in the prompt with an LLM-generated specification docstring, which effectively reduces misguided tests while substantially increasing effective tests, improves multi-round, feedback-driven test generation pipelines, and remains applicable to both buggy and bug-free code.
- Overall, these results suggest that specification-based prompting is a promising strategy for mitigating misguidance from buggy code in LLM-generated unit tests.
Abstract
While Large Language Models (LLMs) show great promise for automating unit test generation, recent studies suggest that the quality of generated tests can be negatively impacted when models are prompted with buggy code. This paper presents a new metric to quantitatively measure the "misguidance effect," a phenomenon where buggy code steers LLMs toward generating tests that validate its erroneous behavior rather than expose it. Our analysis reveals that prompting LLMs with buggy code has a severe, twofold impact: it significantly increases "misguided tests" that assert incorrect behavior while simultaneously suppressing the generation of effective, bug-finding tests. We further corroborate this effect from a model-internal perspective, showing that buggy code skews LLMs' preference toward tests that assert the same erroneous behavior. To counter this, we introduce and validate a specification-based unit test generation paradigm that replaces the code under test in the prompt with an LLM-generated specification docstring. Our results show that this paradigm effectively reduces misguided tests while substantially increasing effective tests, improves multi-round, feedback-driven test generation pipelines, and remains applicable to both buggy and bug-free code. Overall, these results suggest that specification-based prompting is a promising strategy for mitigating misguidance from buggy code in LLM-generated unit tests.
Read the paper
Discussion · 1
Science Skim's AI key points, plus readers' public thoughts
- We present a new metric to quantitatively measure the 'misguidance effect,' a phenomenon where buggy code steers LLMs toward generating tests that validate its erroneous behavior rather than expose it, and our analysis reveals that prompting LLMs with buggy code has a severe, twofold impact: it significantly increases 'misguided tests' that assert incorrect behavior while simultaneously suppressing the generation of effective, bug-finding tests.
- To counter this, we introduce and validate a specification-based unit test generation paradigm that replaces the code under test in the prompt with an LLM-generated specification docstring, which effectively reduces misguided tests while substantially increasing effective tests, improves multi-round, feedback-driven test generation pipelines, and remains applicable to both buggy and bug-free code.
- Overall, these results suggest that specification-based prompting is a promising strategy for mitigating misguidance from buggy code in LLM-generated unit tests.
Readers' thoughts
No reader thoughts yet.
Related papers
- Mutation-Guided Unit Test Generation With a Large Language ModelIEEE Transactions on Software Engineering · 2026
- Do Coverage and Mutation Scores of LLM-Generated Test Suites Correlate with Their Effectiveness? (Replicability Study)Proceedings of the ACM on software engineering. · 2026
- Beyond Coverage and Mutation: A Blind-Spot Analysis of LLM-Generated Unit TestsarXiv · 2026
- On the Risks of using LLM-Generated Tests for Regression TestingarXiv · 2026
- On the Evaluation of Large Language Models in Unit Test Evolution (Experience Paper)Proceedings of the ACM on Software Engineering · 2026
- Security Tests as Executable Specifications for LLM Code Generation: Benefits, Trade-offs, and Coverage LimitsarXiv · 2026