一句话:RATester 借助语言服务器动态查找定义与文档,为 LLM 注入仓库级上下文以生成更优单元测试。
该论文关注 LLM 生成单元测试时的幻觉问题:由于缺乏对整个项目全局上下文的感知,模型难以准确理解焦点方法或函数中出现的标识符。已有工作通常为不同模型和焦点方法提取固定模式的上下文,容易引入大量无关信息,反而导致冗余并干扰模型关注关键内容。为此,作者提出 RATester,集成语言服务器提供动态定义查找:当遇到不熟悉的标识符时,先通过 Gopls 等语言服务器获取其定义与文档注释,再用这些全局知识引导 LLM 生成测试。作者从真实项目构建了新的 Golang 数据集进行评估。结果显示 RATester 平均行覆盖率达 26.25%,相比基线提升 9.10% 至 165.69%;在变异测试中比基线多杀死 18 至 147 个变异体。跨模型、跨编程语言与不同模型规模的实验验证了其模型无关性与通用性。
Recently, Large Language Models (LLMs) have gained attention for their ability to handle a broad range of tasks, including unit test generation. Despite their success, LLMs may exhibit hallucinations when generating unit tests for focal methods or functions due to their lack of awareness regarding the project’s global context. While many studies have explored the role of context, they often extract fixed patterns of context for different models and focal methods, which may not be suitable for all generation processes (e.g., excessive irrelevant context could lead to redundancy, preventing the model from focusing on essential information).To overcome this limitation, we propose RATester, which integrates language servers to provide dynamic definition lookup to assist the LLM. When RATester encounters an unfamiliar identifier, it first leverages language servers (e.g., Gopls) to fetch relevant definitions and documentation comments, and then uses this global knowledge to guide the LLM. We evaluate the effectiveness and efficiency of RATester by constructing a new Golang dataset from real-world projects. On our Golang dataset, RATester achieves an average line coverage of 26.25%, representing an improvement of 9.10% to 165.69% over the baselines. In mutation testing, RATester shows superior performance by successfully killing 18 to 147 more mutants than the baselines. Additionally, our model-agnostic and generalizability analysis confirms RATester’s effectiveness across different models, programming languages, and model scales, validating its broad applicability.
DOI 原文 ·
@inproceedings{paperbot9405,
title = {Enhancing LLM’s Ability to Generate More Repository-Aware Unit Tests Through Precise Context Injection},
author = {Xin Yin and Chao Ni and Xinrui Li and Liushan Chen and Guojun Ma and Xiaohu Yang},
booktitle = {ASE},
year = {2025},
doi = {10.1109/ase63991.2025.00058}
}