Abstract
Unit tests play a vital role in the software development lifecycle. Recent advances in Large Language Model (LLM)-based approaches have significantly improved automated test generation, garnering attention from both academia and industry. We revisit LLM-based unit test generation from a novel perspective by decoupling prefix generation and assertion generation. To characterize their respective challenges, we define Initialization Complexity and adopt Cyclomatic Complexity to measure the difficulty of prefix and assertion generation, revealing that the former primarily affects compilation success, while the latter influences test coverage. To address these challenges, we propose Seed&Steer, a two-step approach that combines traditional unit testing techniques with the capabilities of large language models. Seed&Steer leverages conventional unit testing tools (e.g., EvoSuite) to generate method invocations with high compilation success rates, which serve as seeds to guide LLMs in constructing effective test contexts. It then introduces branching cues to help LLMs explore diverse execution paths (e.g., normal, boundary, and exception cases) and generate assertions with high coverage. We evaluate Seed&Steer on five real-world Java projects against state-of-the-art baselines. Results show that Seed&Steer improves the compilation pass rate by approximately 7%, successfully compiling 792 and 887 previously failing cases on two LLMs. It also achieves up to ~73% branch and line coverage across focal methods of varying complexity, with coverage improvements ranging from 1.09 to 1.26.
Introduction
Automated unit test generation has long been a goal in software engineering. While Large Language Models (LLMs) like ChatGPT are better than traditional tools at writing "human-like" and logical tests, they face two major hurdles:
- Compilation Failures: LLMs often struggle with "Initialization Complexity." They fail to understand complex object dependencies or specific method invocation sequences, resulting in test code that simply won't compile.
- Superficial Coverage: Even when a test compiles, LLMs tend to focus on the "happy path." They struggle to reason through complex "Cyclomatic Complexity" (nested loops and branches), leading to poor branch and line coverage.
The authors argue that these two problems—getting the test to run and getting the test to be thorough—require different types of guidance.
Methodology: The Seed&Steer Framework
The authors propose a two-stage pipeline called Seed&Steer, which decouples the process into setup and verification.
1. The "Seed" Phase (Compilable Prefixes)
To ensure the test actually runs, the framework focuses on generating a valid test prefix (the setup code).
- Mining Invocation Patterns: It uses EvoSuite (a traditional search-based tool) and static analysis of the source code to find real-world examples of how the target method is called.
- Iterative Refinement: If the LLM-generated prefix fails to compile, the system captures the compiler error and feeds it back to the LLM for up to five rounds of "self-healing."
2. The "Steer" Phase (Branch-Guided Assertions)
Once a stable prefix exists, the framework "steers" the LLM to write meaningful assertions.
- Branching Intents: The system performs a static parse of the target method to identify
if-else,switch, andtry-catchblocks. - Natural Language Guidance: It prompts the LLM to describe the conditions needed to trigger these branches in plain English (e.g., "This branch is taken when the input is null").
- Assertion Generation: These "intents" are bundled into the final prompt, forcing the LLM to generate assertions that specifically target different execution paths.
Experimental Setup
The researchers evaluated their approach using Defects4J, a standard benchmark in software engineering research. They selected five representative Java projects: Gson, Commons-Lang, Commons-Cli, Commons-Csv, and JFreeChart. The final dataset consisted of 8,192 focal methods across 666 classes. They compared Seed&Steer against several SOTA baselines, including ChatUniTest, ChatTester, and TestART.
Key Results and Insights
Seed&Steer demonstrated significant improvements across all metrics. The most important insight is that compilability and coverage are distinct problems. Prefix generation (setup) primarily affects whether a test compiles, while assertion generation affects how much of the code is actually tested.
- Compilation Pass Rate: Reached 95.80% using GPT-4o, a ~7% improvement over baselines. It successfully fixed nearly 900 cases that previously failed to compile.
- Code Coverage: Achieved up to 73.30% branch coverage and 75.26% line coverage. This represents a 1.09× to 1.26× improvement over existing methods. As shown in the figure above, Seed&Steer maintains superior performance even as cyclomatic complexity increases.
- Model Agnostic: While it performed best with GPT-4o, the approach also significantly boosted the performance of open-source models like Qwen2.5-Coder.
Limitations and Future Work
Despite its success, Seed&Steer has limitations. Currently, the "Seed" phase relies heavily on EvoSuite, which is strictly tied to Java 8. While the logic is universal, the implementation needs adaptation for modern Java versions or other languages. Additionally, the iterative repair loop introduces a slight execution overhead (averaging 3-6 seconds more per method). Future work will focus on ensuring the semantic correctness of assertions to verify business logic beyond simple code coverage.
Summary updated