arXiv:2602.21997cs.SEcs.AI2026-02被引 1

通过消除已覆盖代码,提升大模型生成单元测试的覆盖率。

Enhancing LLM-Based Test Generation by Eliminating Covered Code

  • 用大模型和静态分析获取复杂函数上下文信息。
  • 迭代生成测试并移除已覆盖代码,最高提升47%覆盖率。
  • 适合需要高覆盖率的复杂代码自动化测试场景。

自动化测试生成对保障软件质量至关重要,覆盖率是衡量测试充分性的关键指标。尽管大语言模型(LLM)在提升测试生成方面展现出潜力,尤其在小规模代码片段上表现优异,但在复杂方法上的应用仍受限。为此,我们提出一种可扩展的基于大模型的单元测试生成方法。该方法包含两个关键步骤:首先,利用大模型与静态分析相结合,获取待测复杂方法的相关上下文信息;其次,采用迭代式测试生成与代码消除策略,持续生成单元测试,追踪覆盖情况,并有选择性地移除已覆盖代码段,从而简化测试任务,缓解长上下文带来的令牌限制或推理效率下降问题。在开源项目上的综合评估表明,该方法显著优于当前最先进的基于大模型和基于搜索的测试生成方法,在复杂方法上实现了更高的覆盖率。

原文摘要 · Abstract (English)

Automated test generation is essential for software quality assurance, with coverage rate serving as a key metric to ensure thorough testing. Recent advancements in Large Language Models (LLMs) have shown promise in improving test generation, particularly in achieving higher coverage. However, while existing LLM-based test generation solutions perform well on small, isolated code snippets, they struggle when applied to complex methods under test. To address these issues, we propose a scalable LLM-based unit test generation method. Our approach consists of two key steps. The first step is context information retrieval, which uses both LLMs and static analysis to gather relevant contextual information associated with the complex methods under test. The second step, iterative test generation with code elimination, repeatedly generates unit tests for the code slice, tracks the achieved coverage, and selectively removes code segments that have already been covered. This process simplifies the testing task and mitigates issues arising from token limits or reduced reasoning effectiveness associated with excessively long contexts. Through comprehensive evaluations on open-source projects, our approach outperforms state-of-the-art LLM-based and search-based methods, demonstrating its effectiveness in achieving high coverage on complex methods.

测试生成大模型代码覆盖自动化测试

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。