Editor’s introduction
The study evaluates a two-stage framework, IGTG&R, which first generates test cases using code coverage principles to ensure high executability and coverage, then refines them with large language models to detect deviations between code implementation and design intent. This approach addresses the limitations of existing methods: code coverage-guided generation lacks intent understanding, while language model-based methods struggle with test executability. The framework demonstrates a functional defect identification rate between 65% and 89%, with 100% execution success and 75.8% code coverage. The researchers measure interference from incorrect code in intent analysis using conditional entropy and mitigate it by excluding the focal method body from context during analysis. Findings show IGTG&R outperforms both standalone CGTG and LLMTG methods in detecting design-intent mismatches while maintaining high test execution rates. The evidence does not include long-term performance or real-world deployment results, limiting broader applicability to complex software systems.
Introduction
Unit testing is widely regarded as the cornerstone of ensuring software quality, with its core objective being to verify whether code implementations comply with design intentions and specifications. However, in reality, design intentions and specifications are often not properly and fully presented, such as in the case of missing documentation. Despite the existence of partial documentation, descriptions of design intentions and specifications are often inadequate in terms of elaboration of each focal method, thus failing to support the design of unit tests. Consequently, the majority of contemporary unit test generation methodologies are predicated on the generation of unit test cases derived from the implemented code, with the objective of attaining diverse forms of code coverage. These approaches are referred to as the code coverage-guided unit test generation (CGTG) approach.
Although the CGTG approach can achieve high coverage and high executability, there are inherent limitations due to the fact that test generation and optimization are guided by coverage analysis of the implemented code. Specifically, the CGTG approach takes the implemented code as the sole verification criterion and uses the coverage of the implemented code as the sole evaluation metric for test cases. It is evident that this renders it impossible to ensure that the test logic aligns with the design intent of the focal method. This issue arises due to interference from the implemented code, which prevents the identification of deviations between code functionality, implementation, and design intent (i.e., functional defects). For example, if the actual implementation of the add(x, y) function is x − y instead of x + y, the CGTG approach, which is devoid of intent analysis, will generate tests based on this erroneous code implementation. As a result, the test logic fails to align with the design intent of the add function, and the functional defect remains undetected. Consequently, the CGTG approach is predominantly employed in the context of regression testing.
In instances where design intentions and specifications are absent, a primary challenge for unit test generation approaches is to generate test cases that not only have high coverage and executability but can also identify functional defects, without being affected by the actual code implementation.
In recent years, the large language model-based test generation (LLMTG) approach has enabled test generation through natural language understanding technologies. Although such approaches can conveniently achieve code intent analysis and test case generation, the initial generation of test cases is generally of a low quality, with relatively low coverage and executability, due to the black-box nature of LLMs. Consequently, iterative debugging has become the prevailing technical approach for the LLMTG method. Nevertheless, the question of its stability remains unresolved.
In order to address the aforementioned challenges, a novel intent analysis-guided unit test generation and refinement (IGTG&R) framework is proposed. The process of generating unit tests is divided into two distinct stages: the initial generation of tests and the refinement of these tests. In the test generation stage, the initial test cases are generated using the CGTG approach, with the objective of achieving high coverage and high executability of the test cases. In the test refinement stage, with the objective of identifying functional defects, intent analysis and test case optimization are conducted. These are based on the context of the focal method while excluding its method body. The IGTG&R framework has been demonstrated to effectively achieve complementary advantages between the CGTG approaches and the LLMTG approaches. It has been shown to exhibit high test case executability and high code coverage, as well as the ability to identify functional defects.
Related Works
Unit test generation approaches automatically generate unit test cases for the focal method (the specific method under test during unit testing). Each test case typically consists of two parts: a test prefix and a test oracle. The test prefix typically comprises a sequence of method call statements or assignment statements, the purpose of which is to drive the focal method into a testable state. The test oracle, meanwhile, is generally constituted by a series of assertion statements that are designed to ascertain whether the behavior of the focal method is in accordance with the expected specifications. The study of test generation approaches focuses on the generation of test prefixes that cover a greater number of testable states and on the generation of test oracles to more accurately detect compliance with expected specifications. At present, the majority of unit test generation approaches can be categorized into two distinct classifications: traditional code coverage-guided test generation approaches and large language model-based test generation approaches.
Code coverage-guided test generation (CGTG): The CGTG approach aims to achieve different types of code coverage by automatically generating unit tests using strategies based on search strategies, random strategies, or constraint strategies. For example, Evosuite uses search and genetic algorithms to generate a test suite that maximizes coverage for a given Java class. Randoop employs a feedback-directed random approach, categorizing test cases into pass or fail suites based on execution results, to generate test cases for Java code. Pynguin applies the techniques used by Randoop to Python code, aiming to address the challenges posed by dynamic typing during test generation.
The CGTG approach involves the analysis of code that has already been implemented through various strategies to maximize test case coverage. Although this approach can achieve high coverage and high executability of test cases, it merely utilizes the coverage of the implemented code as the standard for evaluating test cases. This approach is unable to ensure that the test logic remains consistent with the design intent of the focal methods. This may result in the inability to identify functional defects due to interference from the implemented code.
In comparison with the CGTG approach, the unit test logic generated by the IGTG&R approach, notably the test assertions, remains consistent with the design intent of the code. Conversely, it does not necessarily align with the implemented method’s body code, thereby ensuring the identification of functional defects in the focal method.
Large language model-based test generation (LLMTG): The advent of artificial neural network technology has precipitated a surge in research endeavors concerning the development of test generation approaches founded upon deep learning. These approaches mostly treat unit test generation as a machine translation problem, translating the given focal method into its corresponding test prefix and test oracle. In recent years, LLMTG approaches have gradually emerged. They fine-tune pretrained models on test generation tasks by fully leveraging the capabilities of large language models to generate more human-like and meaningful test cases, demonstrating the enormous potential of large language models in unit test generation.
IGTG&R Framework
IGTG&R is an intent analysis-guided unit test generation and refinement framework. It has been developed to address issues identified in the CGTG approach, including its inability to identify functional defects, and the unstable quality of the LLMTG approach.
STEP 1: Identify focal code: The IGTG&R framework accepts the root directory of the focal project as the initial input, recursively traverses all subdirectories under the specified root directory, and searches for.py files that comply with Python package standards (i.e., directories containing the __init__.py file or conforming to namespace package standards). IGTG&R then converts their paths into dot-separated module names (e.g., mypackage.subpackage corresponds to the subpackage.py file under the mypackage folder in the root directory of the focal project). The list of focal modules is employed for the purpose of defining the scope of code analysis and test generation.
STEP 2: Generate initial test cases based on CGTG: Utilizing random strategies and genetic algorithms, the generation of initial test cases is driven by code coverage analysis of the focal code. Firstly, random strategies are employed to generate a population of test prefixes. These are then checked for validity to ensure that they can execute normally without failing due to syntax or other issues. A fitness function is then defined based on code coverage of the focal code. The genetic algorithm performs selection, crossover, mutation, and other operations on the population of test prefixes, iteratively achieving test cases with high code coverage.
It is noteworthy that during the generation of test cases using CGTG, continuous checks for executability and code coverage are performed on the test cases against the implemented focal code. This approach is intended to guarantee that the generated test cases exhibit both high executability and high code coverage. As was stated previously, CGTG’s limitation can be attributed to its utilization of the implemented code as the exclusive validation criterion. This approach is inadequate in ensuring semantic consistency between code implementation and design intent. Therefore, in STEP 3 and STEP 4, intent analysis and test refinement will be conducted based on the context of the focal code. This will serve to reduce the interference of code implementation on test generation and ensure that the verification logic of the generated tests aligns with the design intent. The outcome of this process is the ability to identify functional defects in the focal code.
Experimental Design
RQ1: How much impact does the exclusion of the method body have on intent analysis of the focal method? The purpose of the IGTG&R approach is to generate test cases capable of identifying functional defects; that is to say, it assumes that the implemented method body may have possibilities that do not meet the design intent. Consequently, when conducting intent analysis, the context of the focal method provided to the LLM does not include the implemented method body to avoid interference with intent analysis and test generation. The fundamental question of IGTG&R is whether the intent of the focal method can be accurately analyzed in the absence of the method body. We design a comparative experiment to evaluate the impact of excluding the method body on intent analysis. The experiment will make a comparison between the semantic consistency of the results of intent analysis with and without the method body.
RQ2: Can the tests generated by IGTG&R effectively detect functional defects? The practical effectiveness of IGTG&R in identifying functional defects will be evaluated by assessing the test cases generated by IGTG&R. In the experiment, predefined functional defects are artificially injected into the focal project. Subsequently, the test cases generated by the IGTG&R framework are executed, and the results are evaluated in order to ascertain the extent to which these test cases can identify the predefined functional defects.
RQ3: How do the test cases generated by IGTG&R perform in terms of the Execution Success Rate (ESR) and the Code Coverage Rate (CCR)? The ESR and CCR are two important metrics for the quality of test cases. Nevertheless, the initial set of test cases generated by LLMTG is frequently regarded as being of substandard quality, along with an inadequate ESR and CCR. This is a key issue that IGTG&R aims to address. The objective of the experiment is to make a comparison and analysis of the improvement effects of the IGTG&R framework in relation to the CGTG and LLMTG approaches using the metrics of the ESR and CCR.
The experimental environment for tool deployment and execution is specified as follows: one server with an Intel Xeon Silver 4310 processor and an NVIDIA A100-SXM-80GB GPU, as well as equipped with the NVIDIA Driver 535.104.05, CUDA 12.2, Ubuntu 22.04 LTS, and Python 3.10. Pynguin-0.39.0 is locally deployed, and CodeLlama (tag: codellama:34b, date: August 2023) and DeepSeek (tag: deepseek-coder:33b, date: October 2023) are locally deployed via Ollama-0.13.0. ChatGPT (tag: gpt-3.5-turbo-0613, date: November 2022) is accessed via OpenAI’s Chat Completions API version 1.0.
Discussion and Future Outlook
Existing unit test generation approaches are predominantly divided into two paradigms: CGTG and LLMTG. CGTG approaches are good at producing test cases with high executability and code coverage but fail to align with design intent, limiting their ability to detect functional defects. LLMTG approaches leverage natural language understanding to capture code intent but suffer from low executability and unstable quality due to the black-box nature of LLMs. The proposed IGTG&R framework combines the advantages of CGTG and LLMTG while also compensating for their respective shortcomings. In comparison with the CGTG approach, the unit test logic generated by the IGTG&R approach, notably the test assertions, remains consistent with the design intent of the code. Conversely, it does not necessarily align with the implemented method’s body code, thereby ensuring the identification of functional defects in the focal method. In comparison with the emerging LLMTG approach, we innovatively propose the exclusion of the implemented method body from the LLM prompts. This is conducted to reduce the interference of code implementation on the analysis of the intent of the focal method and the generation of tests. The illustrative example confirmed that IGTG&R is effective in identifying functional defects. The statistical data from the evaluation experiment demonstrated that the identification rate of functional defects ranged from 65% to 89%. Furthermore, IGTG&R has been demonstrated to attain a higher Execution Success Rate and Code Coverage Rate, with an Execution Success Rate of 100% and a Code Coverage Rate of 75.8%. This impressive performance has been inherited from CGTG and further enhanced by the intent analysis capability of LLMTG.
Internal validity: In recent years, various types of LLMs have been emerging one after another, and their capabilities have also been continuously enhanced. More recent LLMs have demonstrated significant advancements in code understanding, intent prediction accuracy, and reductions in hallucinations, which could potentially improve intent analysis and test refinement in IGTG&R. However, the core architecture of IGTG&R—leveraging CGTG for high executability/coverage and LLMs for intent-driven refinement—remains robust regardless of model generation. The framework’s critical design choice of excluding method bodies to mitigate implementation interference is equally relevant for newer models, as erroneous code still poses bias risks. While updated LLMs may enhance defect detection precision, they would not alter the framework’s fundamental effectiveness, ensuring the study’s core conclusions remain generalizable.
External validity: Another threat lies in the representativeness of the experimental dataset. The selected projects vary intentionally in code size (85–1715 LOCs), the number of modules (2–36), the number of methods (16–278), and application domains, and they are all real Python projects. This diversity aligns with similar unit test generation studies, which often use 4–6 projects for evaluation. While expanding to more projects or additional domains could further strengthen generalizability, the consistent performance of IGTG&R across the diverse sample demonstrates its applicability to common Python project scenarios, mitigating risks of dataset limitation.
Construct validity: The construction of ‘functional defects’ is achieved through the process of code mutations (e.g., arithmetic operator replacement or statement deletion). However, this may not fully capture all real-world functional defects (e.g., logical flaws in complex business logic that do not manifest in input–output mismatches). As observed in RQ2, IGTG&R fails to detect defects in non-core logic because the design intent analysis is focused on core functionality, overlooking auxiliary features. Presently, a satisfactory resolution remains elusive. Introducing code annotation or supplementing function descriptions might be a potential solution.
In the future, the IGTG&R framework can be integrated into the broader paradigm of continuous, interactive learning in software engineering, making it a foundational component of an evolving software ecosystem. This also aligns with the latest advancements in AI-driven software engineering. A key direction is the incorporation of Reinforcement Learning with Human Feedback (RLHF), which draws on foundational work in instruction following and crowd-sourced feedback alignment for code generation. This formalizes developers’ assessments of generated tests into structured reward signals, creating a closed-loop system that iteratively fine-tunes the LLM refinement stage to better align with expert priorities. For instance, feedback on whether a test effectively uncovers critical functional defects or avoids redundant assertions will update the reward function. This guides the LLM to generate more practically useful test oracles over time and ultimately optimizes code generation. Beyond RLHF integration, IGTG&R will leverage context-based Retrieval-Augmented Generation (RAG) to enrich intent analysis and test refinement. By retrieving relevant historical test cases, code documentation, and developer feedback from the project’s evolving ecosystem, the framework will enhance the contextual awareness of both the CGTG initial generation and LLM refinement stages. This synergy of RLHF and RAG will enable IGTG&R to adapt to project-specific coding standards, debugging workflows, and domain-specific defect patterns, transforming it into a dynamic, collaborative tool that grows with the software lifecycle. Additionally, extending this interactive learning paradigm to cross-language support and integration with CI/CD pipelines will further solidify IGTG&R’s role as a foundational technology for AI-assisted software testing, bridging the gap between automated test generation and real-world developer needs.


