Skip to main content
QUICK REVIEW

[论文解读] Code-Aware Prompting: A study of Coverage Guided Test Generation in Regression Setting using LLM

Gabriel Ryan, Siddhartha Jain|arXiv (Cornell University)|Jan 31, 2024
Machine Learning and Data ClassificationComputer Science被引用 5
一句话总结

SymPrompt,是一个具备代码感知的多阶段提示框架,利用静态路径约束和上下文信息来引导大模型生成覆盖率更高的回归测试,在 Python 基准测试中优于基线提示和 SBST,以及 GPT-4。

ABSTRACT

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.

研究动机与目标

  • 激发在 SBST 难以处理复杂分支的回归场景中的自动化测试生成。
  • 引入一个代码感知的多阶段提示策略,在无需额外模型训练的情况下通过 LLM 提高测试输入生成。
  • 利用静态 AST 分析提取路径约束和相关代码上下文,以引导 LLM 生成多样且高覆盖的测试。
  • 在一个 Python 基准上评估 SymPrompt,以量化在多种 LLM 上的正确性、调用准确性和覆盖率的提升。

提出的方法

  • 从 AST 遍历静态收集近似路径约束以表示执行路径。
  • 构建捕捉类型定义和外部依赖的上下文,以支撑测试生成。
  • 迭代的、针对路径的提示生成,请求针对单个执行路径的测试。
  • 使用最小路径(基线路径)集合,在保持覆盖率的同时避免路径数量呈指数级增长。
  • 与 TreeSitter 集成用于解析,并与 CodeGen2 及 GPT-4 提示兼容。
  • 在开源 Python 项目中的 897 个聚焦方法上进行评估,以将 SymPrompt 与 SBST 基线和基线 LLM 提示进行比较。
Figure 1 . Example test generations from an SBST tool (Pynguin), zero shot LLM (CodeGen2), and SymPrompt prompts for focal method exists_as in the flutils open source Python project. An SBST approach is unable to generate full coverage tests for this method without special configuration because it i
Figure 1 . Example test generations from an SBST tool (Pynguin), zero shot LLM (CodeGen2), and SymPrompt prompts for focal method exists_as in the flutils open source Python project. An SBST approach is unable to generate full coverage tests for this method without special configuration because it i

实验结果

研究问题

  • RQ1RQ1:相对于简单提示方法,在回归测试生成中,SymPrompt 如何影响测试性能?
  • RQ2RQ2:SymPrompt 在模型训练数据中可能未见的项目上的表现如何?
  • RQ3RQ3:pathConstraint 提示和焦点上下文如何促进性能提升?
  • RQ4RQ4:SymPrompt 是否在如 GPT-4 这样的大模型上提升性能?

主要发现

  • SymPrompt 显著提升了测试生成相对于基线提示的性能,分别使通过率和正确测试生成提升约 4 倍和 5 倍。
  • 路径约束提示在行覆盖率(约 10%)和分支覆盖率(约 4%)方面较基线提示有更高的提升。
  • 考虑模块加载效应(No-Op 基线)时,SymPrompt 实现了约 3 倍的覆盖提升(大约 15% 对 5%)。
  • 在 CodeGen2 上,SymPrompt 将相对覆盖提高 26%;在 GPT-4 上,覆盖提升超过一倍(超过 2x,约 105% 相对提升)。
  • SymPrompt 降低了聚焦方法调用错误,并通过引导输入测试额外的较少见路径来扩展覆盖(如块设备、套接字)。
  • 未使用 SymPrompt 的基线测试生成常常无法覆盖复杂分支,而 SymPrompt 能在多样化输入上实现更完整的测试。
Figure 2 . Workflow for generating path constraint prompts. The focal method shown in Figure 1(a) is first parsed and its abstract syntax tree is traversed in preorder. In step \raisebox{-.9pt} {1}⃝ , the traversal first visits the first method statement, normalize_path(path) , but does not record a
Figure 2 . Workflow for generating path constraint prompts. The focal method shown in Figure 1(a) is first parsed and its abstract syntax tree is traversed in preorder. In step \raisebox{-.9pt} {1}⃝ , the traversal first visits the first method statement, normalize_path(path) , but does not record a

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。