一句话:将复杂焦点方法切片后让 LLM 逐片生成单元测试,显著提升行与分支覆盖率。
LLM 在 Java 单元测试生成上表现良好,但对包含大量条件与循环的复杂焦点方法覆盖较差:现有方法将整个方法直接交给 LLM,缺少输入分析辅助,模型难以推断覆盖所有条件所需的测试输入,造成行与分支遗漏。本文提出 HITS,将焦点方法分解为切片,并让 LLM 逐切片生成测试用例,从而缩小需分析的范围,使每片中的行与分支更易被覆盖。作者从已有 SOTA 方法所使用的项目中收集复杂焦点方法构建数据集。实验表明,该方法在行覆盖率和分支覆盖率上均显著优于现有基于 LLM 的测试生成方法以及典型的基于搜索的软件测试方法 EvoSuite。
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.
DOI 原文 ·
@inproceedings{paperbot8324,
title = {HITS: High-coverage LLM-based Unit Test Generation via Method Slicing},
author = {Zejun Wang and Kaibo Liu and Ge Li and Zhi Jin},
booktitle = {ASE},
year = {2024},
doi = {10.1145/3691620.3695501}
}