一句话:提出 RUG,结合语义感知自底向上上下文构建与覆盖率引导 fuzzing,让 LLM 为 Rust 项目自动生成可编译且高覆盖率的单元测试。
单元测试能提升软件质量,但编写耗时,开发者需构造合适的测试上下文并确定输入以覆盖不同代码区域。Rust 因其复杂类型系统使传统单元测试生成工具难以奏效,而直接用简单提示让 LLM 生成测试往往产生编译错误,且覆盖率有限。为此,作者设计并实现了 RUG,一种面向 Rust 项目的端到端单元测试自动生成方案。RUG 采用语义感知的自底向上方法,将上下文构建问题分解为有依赖关系的子问题,依次交由 LLM 求解并合并为完整上下文,以通过 Rust 严格编译检查;同时将覆盖率引导 fuzzing 与 LLM 结合,用于准备 fuzzing harness 以提高测试覆盖率。在 17 个真实 Rust 程序(平均约 24,937 LoC)上,RUG 达到最高 71.37% 的代码覆盖率,接近人工的 73.18%;其生成的 113 个覆盖新代码的单元测试中,53 个被接受,17 个被拒绝,43 个仍在评审中。
Unit testing improves software quality by evaluating isolated sections of the program. This approach alleviates the need for comprehensive program-wide testing and confines the potential error scope within the software. However, unit test development is time-consuming, requiring developers to create appropriate test contexts and determine input values to cover different code regions. This problem is particularly pronounced in Rust due to its intricate type system, making traditional unit test generation tools ineffective in Rust projects. Recently, large language models (LLMs) have demonstrated their proficiency in understanding programming language and completing software engineering tasks. However, merely prompting LLMs with a basic prompt like “generate unit test for the following source code” often results in code with compilation errors. In addition, LLM-generated unit tests often have limited test coverage. To bridge this gap and harness the capabilities of LLM, we design and implement RUG, an end-to-end solution to automatically generate the unit test for Rust projects. To help LLM's generated test pass Rust strict compilation checks, RUG designs a semantic-aware bottom-up approach to divide the context construction problem into dependent sub-problems. It solves these sub-problems sequentially using an LLM and merges them to a complete context. To increase test coverage, RUG integrates coverage-guided fuzzing with LLM to prepare fuzzing harnesses. Applying RUG on 17 real-world Rust programs (average$24,937 \text{LoC}$), we show that RUG can achieve a high code coverage, up to$\mathbf{7 1. 3 7 \%}$, closely comparable to human effort$(\mathbf{7 3. 1 8 \%})$. We submitted 113 unit tests generated by RUG covering the new code: 53 of them have been accepted, 17 rejected, and 43 are pending for review.
DOI 原文 ·
@inproceedings{paperbot8731,
title = {Rug: Turbo Llm for Rust Unit Test Generation},
author = {Xiang Cheng and Fan Sang and Yizhuo Zhai and Xiaokuan Zhang and Taesoo Kim},
booktitle = {ICSE},
year = {2025},
doi = {10.1109/icse55347.2025.00097}
}