arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2302.06527cs.SEcs.AI
收藏

使用大型语言模型进行自动化单元测试生成的实证评估

An Empirical Evaluation of Using Large Language Models for Automated Unit Test Generation

University of Alberta(阿尔伯塔大学);University of Stuttgart(斯图加特大学);Northeastern University(东北大学)

Max Schäfer, Sarah Nadi, Aryaz Eghbali, Frank Tip

AI总结:

本文提出TestPilot,利用LLM依据函数签名、实现和文档示例自动生成JavaScript单元测试,并通过失败信息重提示修复;在25个npm包上验证其覆盖率显著优于Nessie且跨模型有效。

摘要:

单元测试在确保软件正确性方面发挥着关键作用。然而,手动创建单元测试是一项繁重的任务,因此需要自动化。最近,大型语言模型(LLM)已被应用于这一问题,方法是利用额外训练或基于现有测试示例的少样本学习。本文对LLM在无需额外训练或人工投入的情况下进行自动化单元测试生成的有效性开展了大规模实证评估,向LLM提供被测函数的签名和实现,以及从文档中提取的用法示例。我们还尝试通过将失败测试和错误消息重新提示给模型来修复生成失败的测试。我们在TestPilot中实现了该方法,这是一款面向JavaScript的测试生成工具,可自动为npm包中的所有API函数生成单元测试。我们使用OpenAI的gpt3.5-turbo LLM,在包含总共1,684个API函数的25个npm包上评估TestPilot。生成测试的中位语句覆盖率达到70.2%,分支覆盖率达到52.8%,显著优于Nessie这一近期的反馈导向JavaScript测试生成技术,后者仅实现51.3%的语句覆盖率和25.6%的分支覆盖率。我们还发现,TestPilot生成的测试中有92.8%与现有测试的相似度不超过50%(按归一化编辑距离衡量),且没有任何测试是完全复制。最后,我们使用另外两个LLM运行TestPilot,即OpenAI较旧的code-cushman-002 LLM和开放LLM StarCoder。总体而言,我们观察到前者结果相近(中位语句覆盖率68.2%),后者结果稍差(中位语句覆盖率54.0%),这表明该方法的有效性受LLM规模和训练集影响,但并不从根本上依赖于特定模型。

英文摘要:

Unit tests play a key role in ensuring the correctness of software. However, manually creating unit tests is a laborious task, motivating the need for automation. Large Language Models (LLMs) have recently been applied to this problem, utilizing additional training or few-shot learning on examples of existing tests. This paper presents a large-scale empirical evaluation on the effectiveness of LLMs for automated unit test generation without additional training or manual effort, providing the LLM with the signature and implementation of the function under test, along with usage examples extracted from documentation. We also attempt to repair failed generated tests by re-prompting the model with the failing test and error message. We implement our approach in TestPilot, a test generation tool for JavaScript that automatically generates unit tests for all API functions in an npm package. We evaluate TestPilot using OpenAI's gpt3.5-turbo LLM on 25 npm packages with a total of 1,684 API functions. The generated tests achieve a median statement coverage of 70.2% and branch coverage of 52.8%, significantly improving on Nessie, a recent feedback-directed JavaScript test generation technique, which achieves only 51.3% statement coverage and 25.6% branch coverage. We also find that 92.8% of TestPilot's generated tests have no more than 50% similarity with existing tests (as measured by normalized edit distance), with none of them being exact copies. Finally, we run TestPilot with two additional LLMs, OpenAI's older code-cushman-002 LLM and the open LLM StarCoder. Overall, we observed similar results with the former (68.2% median statement coverage), and somewhat worse results with the latter (54.0% median statement coverage), suggesting that the effectiveness of the approach is influenced by the size and training set of the LLM, but does not fundamentally depend on the specific model.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑