Automated unit test generation is essential for robust software development, yet existing approaches struggle to generalize across multiple programming languages and operate within real-time development. While Large Language Models (LLMs) offer a promising solution, their ability to generate high coverage test code depends on prompting a concise context of the focal method. Current solutions, such as Retrieval-Augmented Generation, either rely on imprecise similarity-based searches or demand the creation of costly, language-specific static analysis pipelines. To address this gap, we present LSPRAG, a framework for concise-context retrieval tailored for real-time, language-agnostic unit test generation. LSPRAG leverages off-the-shelf Language Server Protocol (LSP) back-ends to supply LLMs with precise symbol definitions and references in real time. By reusing mature LSP servers, LSPRAG provides an LLM with language-aware context retrieval, requiring minimal per-language engineering effort. We evaluated LSPRAG on open-source projects spanning Java, Go, and Python. Compared to the best performance of baselines, LSPRAG increased line coverage by up to 174.55% for Golang, 213.31% for Java, and 31.57% for Python.
翻译:自动化单元测试生成对于稳健的软件开发至关重要,然而现有方法难以在多种编程语言间泛化,且无法在实时开发环境中有效运行。尽管大型语言模型(LLMs)提供了有前景的解决方案,但其生成高覆盖率测试代码的能力依赖于对目标方法简洁上下文的精准提示。当前解决方案(如检索增强生成技术)要么依赖基于相似度的模糊检索,要么需要构建成本高昂的、针对特定语言的静态分析流水线。为弥补这一不足,我们提出了LSPRAG——一个专为实时、跨语言的单元测试生成设计的简洁上下文检索框架。LSPRAG利用现成的语言服务器协议(LSP)后端,实时为LLMs提供精确的符号定义与引用信息。通过复用成熟的LSP服务器,LSPRAG为LLM提供具备语言感知能力的上下文检索,仅需极少的语言适配工程成本。我们在涵盖Java、Go和Python的开源项目上评估了LSPRAG。与基线方法的最佳性能相比,LSPRAG将代码行覆盖率提升了:Go语言最高达174.55%,Java语言最高达213.31%,Python语言最高达31.57%。