arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2511.14224cs.SE
收藏

知识很重要:将项目和测试知识注入基于大语言模型的单元测试生成

Knowledge Matters: Injecting Project and Testing Knowledge into LLM-based Unit Test Generation

Sun Yat-sen University(中山大学)

Anji Li, Mingwei Liu, Zhenxi Chen, Zheng Pei, Zike Li, Dekun Dai, Yanlin Wang, Zibin Zheng

AI总结:

本文提出KTester框架,通过整合项目特定知识和测试领域知识,提升基于大语言模型的单元测试生成能力,实验表明其在多个关键指标上优于现有方法,提高了测试通过率和覆盖率,且生成的测试更易读易维护。

摘要:

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

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑