LSPRAG: LSP-Guided RAG for Language-Agnostic Real-Time Unit Test Generation
Gwihwan Go, Quan Zhang, Chijin Zhou, Zhao Wei, Yu Jiang
TL;DR
LSPRAG tackles the challenge of real-time unit test generation across multiple programming languages by leveraging Language Server Protocol (LSP) back-ends to provide precise, concise context for large language models. It introduces a three-stage design: (1) key token extraction via lexical LSP data and AST structure to focus on branch-relevant symbols, (2) RAG-based context assembly using symbol and location information with a structured prompt, and (3) a compile-free self-repair loop using LSP diagnostics to fix syntax errors in generated tests. Empirical evaluation across Java, Go, and Python shows substantial improvements in line coverage (up to 213.31% in Java, 174.55% in Go, 31.57% in Python) and significant gains in valid test rates, with the approach maintaining practical latency (~28 seconds per method) and acceptable token costs. The work demonstrates that reusing mature language servers for concise, language-aware context retrieval enables robust, real-time, multi-language test generation, and provides an extensible framework that can be adapted to new languages with modest effort.
Abstract
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.
