[论文解读] Can Large Language Models Write Good Property-Based Tests?
该论文研究使用基于GPT-4的提示从API文档合成基于属性的测试(PBT),引入三种提示策略的PBT-GPT,并提出评估生成器和属性质量的方法。
Property-based testing (PBT), while an established technique in the software testing research community, is still relatively underused in real-world software. Pain points in writing property-based tests include implementing diverse random input generators and thinking of meaningful properties to test. Developers, however, are more amenable to writing documentation; plenty of library API documentation is available and can be used as natural language specifications for PBTs. As large language models (LLMs) have recently shown promise in a variety of coding tasks, we investigate using modern LLMs to automatically synthesize PBTs using two prompting techniques. A key challenge is to rigorously evaluate the LLM-synthesized PBTs. We propose a methodology to do so considering several properties of the generated tests: (1) validity, (2) soundness, and (3) property coverage, a novel metric that measures the ability of the PBT to detect property violations through generation of property mutants. In our evaluation on 40 Python library API methods across three models (GPT-4, Gemini-1.5-Pro, Claude-3-Opus), we find that with the best model and prompting approach, a valid and sound PBT can be synthesized in 2.4 samples on average. We additionally find that our metric for determining soundness of a PBT is aligned with human judgment of property assertions, achieving a precision of 100% and recall of 97%. Finally, we evaluate the property coverage of LLMs across all API methods and find that the best model (GPT-4) is able to automatically synthesize correct PBTs for 21% of properties extractable from API documentation.
研究动机与目标
- 动机:在现实世界的软件中较少使用基于属性的测试(PBT),并识别生成生成器和有意义属性时的难点。
- 提出一种方法(PBT-GPT),使用大型语言模型从API文档合成PBT组件。
- 引入三种提示策略——独立、连续和联合,用于生成器和属性的合成。
- 制定评估方法学,用于评估生成器的有效性/多样性以及属性的有效性/正确性/强度。
- 提供关于Python库API(numpy、networkx、datetime)的初步结果,以说明潜在收益与局限性。
提出的方法
- 设计一个提示模板,其中包含API文档、系统/用户指令,以及用于生成器、属性,或两者的指定输出格式。
- 定义三种提示方法——独立为生成器和属性提示、带上下文的连续提示、以及联合提示以生成一个综合测试。
- 概述PBT-GPT的失效模态,并提出以生成器质量(有效性与多样性)和属性质量(有效性、正确性与强度)为重点的评估方法。
- 在示例Python API上实现并评估PBT-GPT,使用Hypothesis作为PBT框架。
- 讨论缓解策略和人机协作方法,以提高生成器的有效性、多样性以及属性的正确性。

实验结果
研究问题
- RQ1LLMs是否能够从API文档合成可用的基于属性的测试?
- RQ2不同的提示策略如何影响生成的PBT组件(生成器和属性)的质量?
- RQ3在LLM生成的PBT中有哪些常见的失效模式,如何缓解?
- RQ4如何评估合成PBT的生成器有效性/多样性以及属性有效性/正确性/强度?
- RQ5将PBT-GPT应用于numpy、networkx和datetime API时可以观察到哪些初步结果?
主要发现
- PBT-GPT在从API文档推导的生成器和属性方面取得了有前景的初步结果,覆盖numpy、networkx和datetime。
- 三种提示策略(独立、连续、联合)在生成器和属性的合成之间提供不同的权衡。
- 生成器可能存在有效性和多样性问题;属性可能无效、不正确或薄弱,需要缓解或人工参与的 refinement。
- 提出并展示一个聚焦于生成器有效性、生成器多样性、属性有效性、属性正确性和属性强度的评估方法学。
- 缓解策略包括持续提示以修复无效的生成器/属性,并使用示例以提高正确性和强度。
- 初步结果表明,基于LLM合成的PBT可以作为开发者迭代测试的有用起点。

阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。