Seed&Steer:利用可编译前缀与分支信号引导大语言模型进行单元测试生成
Seed&Steer: Guiding Large Language Models with Compilable Prefix and Branch Signals for Unit Test Generation
AI总结:
本文提出Seed&Steer方法,通过解耦前缀与断言生成,利用传统测试工具生成高编译成功率的方法调用作为种子,并结合分支提示引导LLM探索多样执行路径,在五个Java项目上将编译通过率提升约7%,覆盖率提升最高达1.26倍。
摘要:
单元测试在软件开发生命周期中起着至关重要的作用。近年来,基于大语言模型(LLM)的方法取得了显著进展,大幅提升了自动化测试生成能力,引起了学术界和工业界的广泛关注。我们从解耦前缀生成与断言生成的全新视角重新审视基于LLM的单元测试生成。为了刻画二者各自的挑战,我们定义了初始化复杂度(Initialization Complexity),并采用圈复杂度(Cyclomatic Complexity)来衡量前缀生成和断言生成的难度,结果表明前者主要影响编译成功率,而后者影响测试覆盖率。为应对这些挑战,我们提出了Seed&Steer,一种将传统单元测试技术与大语言模型能力相结合的两步方法。Seed&Steer利用传统单元测试工具(如EvoSuite)生成具有高编译成功率的方法调用,并将其作为种子引导LLM构建有效的测试上下文。随后,该方法引入分支提示(branching cues),帮助LLM探索多样化的执行路径(如正常、边界和异常情况),并生成具有高覆盖率的断言。我们在五个真实Java项目上将Seed&Steer与最先进的基线方法进行了评估。结果表明,Seed&Steer将编译通过率提高了约7%,在两个LLM上成功编译了792个和887个此前失败的用例。在不同复杂度的焦点方法上,它实现了最高约73%的分支覆盖率和行覆盖率,覆盖率提升幅度为1.09倍至1.26倍。我们的代码、数据集和实验脚本将公开发布,以支持未来研究和可复现性。
英文摘要:
Unit tests play a vital role in the software development lifecycle. Recent advances in Large Language Model (LLM)-based approaches have significantly improved automated test generation, garnering attention from both academia and industry. We revisit LLM-based unit test generation from a novel perspective by decoupling prefix generation and assertion generation. To characterize their respective challenges, we define Initialization Complexity and adopt Cyclomatic Complexity to measure the difficulty of prefix and assertion generation, revealing that the former primarily affects compilation success, while the latter influences test coverage. To address these challenges, we propose Seed&Steer, a two-step approach that combines traditional unit testing techniques with the capabilities of large language models. Seed&Steer leverages conventional unit testing tools (e.g., EvoSuite) to generate method invocations with high compilation success rates, which serve as seeds to guide LLMs in constructing effective test contexts. It then introduces branching cues to help LLMs explore diverse execution paths (e.g., normal, boundary, and exception cases) and generate assertions with high coverage. We evaluate Seed&Steer on five real-world Java projects against state-of-the-art baselines. Results show that Seed&Steer improves the compilation pass rate by approximately 7%, successfully compiling 792 and 887 previously failing cases on two LLMs. It also achieves up to ~73% branch and line coverage across focal methods of varying complexity, with coverage improvements ranging from 1.09* to 1.26*. Our code, dataset, and experimental scripts will be publicly released to support future research and reproducibility.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。