HITS: High-coverage LLM-based Unit Test Generation via Method Slicing

Zejun Wang,Kaibo Liu,Ge Li,Zhi Jin
2024-08-21
Abstract:Large language models (LLMs) have behaved well in generating unit tests for Java projects. However, the performance for covering the complex focal methods within the projects is poor. Complex methods comprise many conditions and loops, requiring the test cases to be various enough to cover all lines and branches. However, existing test generation methods with LLMs provide the whole method-to-test to the LLM without assistance on input analysis. The LLM has difficulty inferring the test inputs to cover all conditions, resulting in missing lines and branches. To tackle the problem, we propose decomposing the focal methods into slices and asking the LLM to generate test cases slice by slice. Our method simplifies the analysis scope, making it easier for the LLM to cover more lines and branches in each slice. We build a dataset comprising complex focal methods collected from the projects used by existing state-of-the-art approaches. Our experiment results show that our method significantly outperforms current test case generation methods with LLMs and the typical SBST method Evosuite regarding both line and branch coverage scores.
Software Engineering
What problem does this paper attempt to address?
### What problem does this paper attempt to solve? This paper aims to solve the low - coverage problem in the automatic unit test generation of complex methods (i.e., functions containing multiple conditions and loops). Specifically: 1. **Limitations of existing methods**: - Existing unit test generation methods based on large - language models (LLMs) perform poorly when dealing with complex methods and are unable to cover all code lines and branches. - These methods usually directly provide the entire method to the LLM for test - case generation without providing assistance for input analysis, making it difficult for the LLM to infer test inputs that can cover all conditions. 2. **Characteristics of complex methods**: - Complex methods usually have a high cyclomatic complexity, which means they contain many conditional and loop structures. - In order to ensure high coverage, test cases need to be diverse enough to cover all possible execution paths. 3. **The proposed new method**: - To solve the above problems, the authors proposed HITS (High - coverage LLM - based Unit Test Generation via Method Slicing), a method that decomposes complex methods through method slicing and generates test cases slice by slice. - HITS simplifies the analysis scope of the LLM in each generation round by decomposing complex methods into smaller code slices, thus making it easier to cover more code lines and branches. 4. **Experimental verification**: - The authors constructed a data set containing complex methods and experimentally verified the significant advantages of HITS in line coverage and branch coverage over existing LLM - based test generation methods and traditional SBST methods (such as Evosuite). ### Summary The main goal of this paper is to use the method - slicing technique to generate high - coverage unit test cases with large - language models, especially when dealing with complex methods, to improve the diversity and coverage of test cases.