用程序分析辅助LLM生成更精准的Java单元测试
Static Program Analysis Guided LLM Based Unit Test Generation
- 通过静态分析提取方法的关键上下文信息,增强LLM提示
- 在商业与开源项目中显著提升测试生成成功率
- 适合需要自动化测试的Java开发团队使用
我们提出一种基于大语言模型(LLM)的Java方法单元测试自动生成新方法。现有方法依赖被测方法的调用示例或整个类作为输入,前者因缺乏调用实例而不可行,尤其对新编写的方法;后者因上下文过长导致生成困难,难以扩展。本文通过静态程序分析获取聚焦方法的简洁、精确上下文信息,并融入提示中,显著提升了LLM生成单元测试的效果。我们在一个大型商业Java项目和一个流行的开源项目上验证了该方法的有效性。
原文摘要 · Abstract (English)
We describe a novel approach to automating unit test generation for Java methods using large language models (LLMs). Existing LLM-based approaches rely on sample usage(s) of the method to test (focal method) and/or provide the entire class of the focal method as input prompt and context. The former approach is often not viable due to the lack of sample usages, especially for newly written focal methods. The latter approach does not scale well enough; the bigger the complexity of the focal method and larger associated class, the harder it is to produce adequate test code (due to factors such as exceeding the prompt and context lengths of the underlying LLM). We show that augmenting prompts with \emph{concise} and \emph{precise} context information obtained by program analysis %of the focal method increases the effectiveness of generating unit test code through LLMs. We validate our approach on a large commercial Java project and a popular open-source Java project.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。