Automated unit test generation using large language models (LLMs) holds great promise but often struggles with generating tests that are both correct and maintainable in real-world projects. This paper presents KTester, a novel framework that integrates project-specific knowledge and testing domain knowledge to enhance LLM-based test generation. Our approach first extracts project structure and usage knowledge through static analysis, which provides rich context for the model. It then employs a testing-domain-knowledge-guided separation of test case design and test method generation, combined with a multi-perspective prompting strategy that guides the LLM to consider diverse testing heuristics. The generated tests follow structured templates, improving clarity and maintainability. We evaluate KTester on multiple open-source projects, comparing it against state-of-the-art LLM-based baselines using automatic correctness and coverage metrics, as well as a human study assessing readability and maintainability. Results demonstrate that KTester significantly outperforms existing methods across six key metrics, improving execution pass rate by 5.69% and line coverage by 8.83% over the strongest baseline, while requiring less time and generating fewer test cases. Human evaluators also rate the tests produced by KTester significantly higher in terms of correctness, readability, and maintainability, confirming the practical advantages of our knowledge-driven framework.


翻译:基于大语言模型(LLMs)的自动化单元测试生成具有巨大潜力,但在实际项目中往往难以生成既正确又可维护的测试。本文提出KTester,一个集成项目特定知识与测试领域知识以增强基于LLM的测试生成的新框架。我们的方法首先通过静态分析提取项目结构和使用知识,为模型提供丰富的上下文。随后采用测试领域知识引导的测试用例设计与测试方法生成相分离的策略,并结合多视角提示策略,引导LLM综合考虑多样化的测试启发式方法。生成的测试遵循结构化模板,提升了清晰度与可维护性。我们在多个开源项目上评估KTester,通过自动正确性与覆盖率指标以及评估可读性和可维护性的人工研究,与最先进的基于LLM的基线方法进行对比。结果表明,KTester在六项关键指标上显著优于现有方法,相较于最强基线,执行通过率提升5.69%,行覆盖率提升8.83%,同时所需时间更少且生成的测试用例更少。人工评估者也对KTester生成的测试在正确性、可读性和可维护性方面给予显著更高的评分,证实了我们知识驱动框架的实践优势。

0
下载
关闭预览

相关内容

评估大语言模型在科学发现中的作用
专知会员服务
19+阅读 · 2025年12月19日
大语言模型中的检索与结构化增强生成综述
专知会员服务
32+阅读 · 2025年9月17日
定制化大型语言模型的图检索增强生成综述
专知会员服务
37+阅读 · 2025年1月28日
如何检测LLM内容?UCSB等最新首篇《LLM生成内容检测》综述
KnowledGPT:基于知识库的检索和存储访问增强大型语言模型
NLG ≠ 机器写作 | 专家专栏
量子位
13+阅读 · 2018年9月10日
推荐|上交大推出Texygen:文本生成模型的基准测试平台
国家自然科学基金
4+阅读 · 2017年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
2+阅读 · 2014年12月31日
国家自然科学基金
4+阅读 · 2014年12月31日
VIP会员
相关基金
国家自然科学基金
4+阅读 · 2017年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
2+阅读 · 2014年12月31日
国家自然科学基金
4+阅读 · 2014年12月31日
Top
微信扫码咨询专知VIP会员