基于属性检索的LLM单元测试生成
LLM-based Unit Test Generation via Property Retrieval
AI总结:
针对LLM单元测试生成重覆盖率轻实用性的问题,提出基于属性的检索增强机制APT,将测试分为Given、When、Then阶段并引入任务特定上下文检索,在12个开源项目上验证了其在正确性、完整性和可维护性上的优越性。
摘要:
自动化单元测试生成已被广泛研究,大型语言模型(LLMs)近期展现出巨大潜力。此外,在单元测试生成背景下,这些工具优先考虑高代码覆盖率,往往以牺牲实用性、正确性和可维护性为代价。为此,我们提出基于属性的检索增强(Property-Based Retrieval Augmentation),这是一种新颖的机制,将基于LLM的检索增强生成(RAG)扩展到基本的向量、文本相似性和基于图的方法之外。我们的方法考虑了任务特定的上下文,并引入了定制的属性检索机制。具体而言,在单元测试生成任务中,我们考虑了单元测试的独特结构,将测试生成过程分为Given、When和Then阶段。在为焦点方法生成测试时,我们不仅检索被测代码的通用上下文,还考虑任务特定的上下文,例如其他方法已有的测试,这能为Given、When和Then阶段提供有价值的见解。这形成了焦点方法与其他方法之间的属性关系,从而将检索范围扩展到传统RAG之外。我们在名为APT的工具中实现了该方法,该工具依次执行预处理、属性检索和单元测试生成,使用新生成的测试指导后续测试创建的迭代策略。我们在12个开源项目的1515个方法上评估了APT,结果表明APT在生成测试的正确性、完整性和可维护性方面始终优于现有工具。此外,我们为LLM引入了超越通用上下文的、新颖的代码上下文感知检索机制,为其他代码相关任务提供了有价值的见解和潜在应用。
英文摘要:
Automated unit test generation has been widely studied, with Large Language Models (LLMs) recently showing significant potential. Moreover, in the context of unit test generation, these tools prioritize high code coverage, often at the expense of practical usability, correctness, and maintainability. In response, we propose Property-Based Retrieval Augmentation, a novel mechanism that extends LLM-based Retrieval-Augmented Generation (RAG) beyond basic vector, text similarity, and graph-based methods. Our approach considers task-specific context and introduces a tailored property retrieval mechanism. Specifically, in the unit test generation task, we account for the unique structure of unit tests by dividing the test generation process into Given, When, and Then phases. When generating tests for a focal method, we not only retrieve general context for the code under test but also consider task-specific context such as pre-existing tests of other methods, which can provide valuable insights for any of the Given, When, and Then phases. This forms property relationships between focal method and other methods, thereby expanding the scope of retrieval beyond traditional RAG. We implement this approach in a tool called APT, which sequentially performs preprocessing, property retrieval, and unit test generation, using an iterative strategy where newly generated tests guide the creation of subsequent ones. We evaluated APT on 12 open-source projects with 1515 methods, and the results demonstrate that APT consistently outperforms existing tools in terms of correctness, completeness, and maintainability of the generated tests. Moreover, we introduce a novel code-context-aware retrieval mechanism for LLMs beyond general context, offering valuable insights and potential applications for other code-related tasks.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。