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. This paper presents TestPilot, an adaptive test generation technique that leverages Large Language Models (LLMs). TestPilot uses Codex, an off-the-shelf LLM, to automatically generate unit tests for a given program without requiring additional training or few-shot learning on examples of existing tests. In our approach, Codex is provided with prompts that include the signature and implementation of a function under test, along with usage examples extracted from documentation. If a generated test fails, TestPilot's adaptive component attempts to generate a new test that fixes the problem by re-prompting the model with the failing test and error message. We created an implementation of TestPilot for JavaScript and evaluated it on 25 npm packages with a total of 1,684 API functions to generate tests for. Our results show that the generated tests achieve up to 93.1% statement coverage (median 68.2%). Moreover, on average, 58.5% of the generated tests contain at least one assertion that exercises functionality from the package under test. Our experiments with excluding parts of the information included in the prompts show that all components contribute towards the generation of effective test suites. Finally, we find that TestPilot does not generate memorized tests: 92.7% of our generated tests have $\leq$ 50% similarity with existing tests (as measured by normalized edit distance), with none of them being exact copies.
翻译:单位测试在确保软件正确性方面发挥着关键作用 。 然而, 手工创建单位测试是一项艰巨的任务, 需要自动化。 本文展示了 TestPilot, 这是一种利用大语言模型( LLMs) 的适应性测试生成技术。 TestPilot 使用 Scodex, 这是一种现成的 LLM, 自动为特定程序生成单位测试, 不需要额外培训或对现有测试实例进行少镜头学习 。 在我们的方法中, 向代码x 提供提示, 包括测试中函数的签名和执行, 以及从文档中提取的使用示例。 如果生成的测试失败, 测试Pilot 的适应性组件试图生成一个新的测试, 从而通过重新激活失败的测试和错误信息来修正问题。 我们为 JavaScript 创建了测试程序, 并在25 npm 软件包上进行了评估, 总共1 684 API 函数生成测试。 我们的结果表明, 生成的测试结果达到93.1% 的对语标的覆盖率( emern e 68. ) 。 此外, 测试中的平均, 58.5% 测试中包含我们生成的测试的测试中的所有测试中的所有测试中的所有测试中包含所有测试中的所有部分。 。 。 最后测试中包含所有测试的测试的测试中包含所有测试中的测试的测试的测试中的测试中的测试中的测试中的所有部分。