Search papers, labs, and topics across Lattice.
This paper addresses the challenge of applying LLMs to generate unit tests for complex methods by proposing a two-step approach involving context retrieval and iterative test generation with covered code elimination. The method leverages both LLMs and static analysis for context retrieval and iteratively generates tests, removing covered code segments to simplify the testing task and improve reasoning. Experiments on open-source projects demonstrate that the proposed approach outperforms existing LLM-based and search-based methods in achieving high coverage.
LLMs can generate better unit tests for complex code by iteratively removing already-covered code, simplifying the generation task and boosting coverage beyond state-of-the-art methods.
Automated test generation is essential for software quality assurance, with coverage rate serving as a key metric to ensure thorough testing. Recent advancements in Large Language Models (LLMs) have shown promise in improving test generation, particularly in achieving higher coverage. However, while existing LLM-based test generation solutions perform well on small, isolated code snippets, they struggle when applied to complex methods under test. To address these issues, we propose a scalable LLM-based unit test generation method. Our approach consists of two key steps. The first step is context information retrieval, which uses both LLMs and static analysis to gather relevant contextual information associated with the complex methods under test. The second step, iterative test generation with code elimination, repeatedly generates unit tests for the code slice, tracks the achieved coverage, and selectively removes code segments that have already been covered. This process simplifies the testing task and mitigates issues arising from token limits or reduced reasoning effectiveness associated with excessively long contexts. Through comprehensive evaluations on open-source projects, our approach outperforms state-of-the-art LLM-based and search-based methods, demonstrating its effectiveness in achieving high coverage on complex methods.