Code-Aware Prompting: A study of Coverage Guided Test Generation in Regression Setting using LLM
作者: Gabriel Ryan, Siddhartha Jain, Mingyue Shang, Shiqi Wang, Xiaofei Ma, Murali Krishna Ramanathan, Baishakhi Ray
分类: cs.SE, cs.LG
发布日期: 2024-01-31 (更新: 2024-04-02)
💡 一句话要点
提出SymPrompt以解决LLM生成测试用例覆盖率低的问题
🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)
关键词: 软件测试 大型语言模型 代码生成 测试覆盖率 多阶段提示 自动化测试 机器学习
📋 核心要点
- 现有的基于搜索的软件测试方法在复杂软件单元上常常无法实现理想的测试覆盖率,导致测试效果不佳。
- 本文提出SymPrompt,通过将测试用例生成过程分解为多阶段,结合代码执行路径和上下文信息,提升了生成质量。
- 实验表明,SymPrompt在CodeGen2上提高了测试生成的正确性5倍,覆盖率提升26%,在GPT-4上覆盖率提升超过2倍。
📝 摘要(中文)
测试在确保软件质量中发挥着关键作用,但传统的基于搜索的软件测试方法在复杂软件单元上常常表现不佳,导致测试覆盖率低。近期的研究利用大型语言模型(LLMs)进行测试生成,虽然在生成质量上有所提升,但仍采用固定的提示策略,导致生成的测试用例覆盖率不足。本文提出了SymPrompt,一种针对代码的提示策略,通过将测试用例生成过程分解为多阶段序列,结合执行路径和相关上下文,显著提高了测试用例的完整性和覆盖率。实验结果表明,SymPrompt在CodeGen2上提升了测试生成的正确性5倍,覆盖率提升26%。在GPT-4上应用时,覆盖率提升超过2倍。
🔬 方法详解
问题定义:本文旨在解决现有LLM生成测试用例时覆盖率低的问题。传统的测试生成方法在复杂软件单元上表现不佳,导致生成的测试用例无法充分覆盖代码路径。
核心思路:SymPrompt的核心思路是将测试用例生成过程分解为多个阶段,每个阶段通过特定的提示引导模型生成更完整的测试用例。这种多步骤的推理方式能够帮助LLM更好地理解和解决复杂逻辑问题。
技术框架:SymPrompt的整体架构包括使用TreeSitter解析框架进行代码解析,并将测试生成过程分为多个阶段。每个阶段的提示与被测方法的执行路径相对应,同时提供相关的类型和依赖上下文。
关键创新:SymPrompt的主要创新在于其代码感知的提示策略,通过多阶段的提示设计,使得预训练的LLM能够在不进行额外训练的情况下生成更完整的测试用例。这与传统的固定提示策略形成了鲜明对比。
关键设计:在实现过程中,SymPrompt的设计包括对提示的精细调整,以确保每个阶段都能有效引导模型生成高质量的测试用例,同时利用TreeSitter进行代码解析以获取必要的上下文信息。具体的参数设置和损失函数等技术细节在论文中进行了详细描述。
🖼️ 关键图片
📊 实验亮点
实验结果显示,SymPrompt在CodeGen2上提高了测试生成的正确性5倍,覆盖率提升26%。在使用GPT-4时,SymPrompt的覆盖率提升超过2倍,相较于基线提示策略表现出显著的优势。
🎯 应用场景
该研究的潜在应用领域包括软件测试自动化、代码质量保证和持续集成等。通过提高测试用例的生成质量和覆盖率,SymPrompt能够帮助开发团队更有效地发现和修复软件缺陷,从而提升软件的可靠性和安全性。未来,该方法有望在更多编程语言和复杂系统中得到应用,推动软件测试技术的发展。
📄 摘要(原文)
Testing plays a pivotal role in ensuring software quality, yet conventional Search Based Software Testing (SBST) methods often struggle with complex software units, achieving suboptimal test coverage. Recent works using large language models (LLMs) for test generation have focused on improving generation quality through optimizing the test generation context and correcting errors in model outputs, but use fixed prompting strategies that prompt the model to generate tests without additional guidance. As a result LLM-generated testsuites still suffer from low coverage. In this paper, we present SymPrompt, a code-aware prompting strategy for LLMs in test generation. SymPrompt's approach is based on recent work that demonstrates LLMs can solve more complex logical problems when prompted to reason about the problem in a multi-step fashion. We apply this methodology to test generation by deconstructing the testsuite generation process into a multi-stage sequence, each of which is driven by a specific prompt aligned with the execution paths of the method under test, and exposing relevant type and dependency focal context to the model. Our approach enables pretrained LLMs to generate more complete test cases without any additional training. We implement SymPrompt using the TreeSitter parsing framework and evaluate on a benchmark challenging methods from open source Python projects. SymPrompt enhances correct test generations by a factor of 5 and bolsters relative coverage by 26% for CodeGen2. Notably, when applied to GPT-4, SymPrompt improves coverage by over 2x compared to baseline prompting strategies.