Abstract
Automated test generation has evolved with the advent of Large Language Models (LLMs), yet most evaluation benchmarks focus on standalone functions. In real-world software development, the majority of code is "non-standalone," relying on class-level context and external dependencies. This paper presents TestBench, the first benchmark specifically designed to evaluate LLMs on class-level unit test generation. Our study explores the "context paradox," where model performance varies significantly based on the amount of background information provided. We evaluate state-of-the-art models, including GPT-4 and CodeLlama, and introduce a heuristic repair algorithm that dramatically improves the usability of LLM-generated tests.
Introduction
While tools for automated test generation have existed for decades, LLMs have shifted the paradigm toward natural language prompting. However, current benchmarks like HumanEval or TestEval primarily target isolated logic. In practice, over 70% of code is non-standalone, requiring an understanding of class structures, private fields, and inherited methods to write valid tests. TestBench addresses this gap by providing a standardized framework to measure how LLMs handle class-level complexity, evaluating not just syntactic appearance but also compilation, execution, and defect detection capabilities.
Methodology
The TestBench evaluation framework utilizes a three-pronged approach to assess LLM performance:
- Prompt Engineering with Context Levels: To investigate how background information affects generation, three prompt types were designed: Self-contained (function only), Full Context (entire source file), and Simple Context (AST-pruned declarations).
- Fine-Grained Evaluation: Models are assessed across five dimensions: syntactic correctness, compilation, execution, code coverage (via JaCoCo), and defect detection (via PITest mutation testing).
- Heuristic Repair Algorithm: To mitigate common structural errors—such as missing imports or class wrappers—a heuristic tool automatically repairs minor issues before the final evaluation.
Dataset Collection
The TestBench dataset comprises 108 Java programs manually selected from nine high-quality, large-scale GitHub projects. These projects represent diverse domains, including microservices (Apollo), computer vision (JavaCV), and charting libraries (JFreeChart). The selection process ensured that the functions represent realistic development scenarios rather than trivial examples.
Key Insights and Results
The Context Paradox
A primary finding is the "context paradox": more information is not universally beneficial. While GPT-4's performance scaled with increased context, smaller models like CodeLlama-13B were often overwhelmed by "noise" in the Full Context prompts, leading to higher failure rates. For these medium-sized models, the "Simple Context" (declarations only) proved to be the optimal balance.
Performance Benchmarks
- GPT-4 Superiority: GPT-4 emerged as the top performer, achieving a 92.51% line coverage on successful tests and a 26.10% mutation kill rate.
- Compilation Barriers: Compilation remains the most significant hurdle, with errors accounting for 48% to 56% of failures across all models.
- Repair Efficacy: The heuristic repair algorithm was particularly transformative for GPT-3.5, reducing its syntax error rate from 97.84% to just 4.38% by fixing structural "fill-in-the-blank" hallucinations.
Discussion and Conclusion
Despite high coverage scores, the relatively low mutation kill rates (~17-26%) indicate that LLMs still struggle with the logical depth required for rigorous edge-case detection. Future research should focus on advanced context management to help models navigate large codebases and improving the logical reasoning behind test assertions. TestBench provides the necessary foundation for tracking progress in these critical areas of automated software engineering.
Summary updated