用大模型生成更精准的单元测试,覆盖复杂代码路径。
Navigating the Labyrinth: Path-Sensitive Unit Test Generation with Large Language Models
- 结合代码知识与大模型语义能力,生成路径敏感的测试用例。
- 在2258个复杂方法上平均提升分支覆盖率29.60%、行覆盖率31.00%。
- 能发现真实项目中的漏洞,适合开发与质量保障人员使用。
单元测试对软件质量至关重要,但编写和维护耗时且易出错。现有自动化方法多依赖固定启发式规则或有限上下文信息,缺乏对深层控制流结构的推理能力,导致难以覆盖复杂执行路径。为此,本文提出路径敏感框架JUnitGenie,通过从真实Java项目中提取代码知识,并将其提炼为结构化提示,引导大模型生成高覆盖率的单元测试。在来自十个真实项目的2,258个复杂方法上评估显示,JUnitGenie生成的有效测试用例使分支覆盖率平均提升29.60%,行覆盖率提升31.00%,显著优于传统启发式与基于LLM的基线方法。进一步验证表明,生成的测试可发现实际存在的缺陷,已被开发者确认并修复。
原文摘要 · Abstract (English)
Unit testing is essential for software quality assurance, yet writing and maintaining tests remains time-consuming and error-prone. To address this challenge, researchers have proposed various techniques for automating unit test generation, including traditional heuristic-based methods and more recent approaches that leverage large language models (LLMs). However, these existing approaches are inherently path-insensitive because they rely on fixed heuristics or limited contextual information and fail to reason about deep control-flow structures. As a result, they often struggle to achieve adequate coverage, particularly for deep or complex execution paths. In this work, we present a path-sensitive framework, JUnitGenie, to fill this gap by combining code knowledge with the semantic capabilities of LLMs in guiding context-aware unit test generation. After extracting code knowledge from Java projects, JUnitGenie distills this knowledge into structured prompts to guide the generation of high-coverage unit tests. We evaluate JUnitGenie on 2,258 complex focal methods from ten real-world Java projects. The results show that JUnitGenie generates valid tests and improves branch and line coverage by 29.60% and 31.00% on average over both heuristic and LLM-based baselines. We further demonstrate that the generated test cases can uncover real-world bugs, which were later confirmed and fixed by developers.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。