[论文解读] No More Manual Tests? Evaluating and Improving ChatGPT for Unit Test Generation
本论文实证评估 ChatGPT 在 Java 单元测试生成中的表现,分析其优点与局限,并引入 ChatTester,通过初始测试生成器与迭代改进器以提高正确性。
Unit testing is essential in detecting bugs in functionally-discrete program units. Manually writing high-quality unit tests is time-consuming and laborious. Although traditional techniques can generate tests with reasonable coverage, they exhibit low readability and cannot be directly adopted by developers. Recent work has shown the large potential of large language models (LLMs) in unit test generation, which can generate more human-like and meaningful test code. ChatGPT, the latest LLM incorporating instruction tuning and reinforcement learning, has performed well in various domains. However, It remains unclear how effective ChatGPT is in unit test generation. In this work, we perform the first empirical study to evaluate ChatGPT's capability of unit test generation. Specifically, we conduct a quantitative analysis and a user study to systematically investigate the quality of its generated tests regarding the correctness, sufficiency, readability, and usability. The tests generated by ChatGPT still suffer from correctness issues, including diverse compilation errors and execution failures. Still, the passing tests generated by ChatGPT resemble manually-written tests by achieving comparable coverage, readability, and even sometimes developers' preference. Our findings indicate that generating unit tests with ChatGPT could be very promising if the correctness of its generated tests could be further improved. Inspired by our findings above, we propose ChatTESTER, a novel ChatGPT-based unit test generation approach, which leverages ChatGPT itself to improve the quality of its generated tests. ChatTESTER incorporates an initial test generator and an iterative test refiner. Our evaluation demonstrates the effectiveness of ChatTESTER by generating 34.3% more compilable tests and 18.7% more tests with correct assertions than the default ChatGPT.
研究动机与目标
- 评估 ChatGPT 在生成的单元测试中的正确性、充分性、可读性与可用性。
- 识别 ChatGPT 生成的测试中导致编译与执行错误的常见原因。
- 将 ChatGPT 与传统的 Evosuite 与基于学习的 AthenaTest 基线进行比较。
- 提出 ChatTester 以提升测试正确性并展示提升效果。
- 提供在单元测试生成中利用 ChatGPT 的实践指南。
提出的方法
- 从 1,748 对中构建涵盖 185 个 Java 项目的 1,000 对焦方法–测试方法数据对的基准。
- 设计一个两部分的基本提示(自然语言任务描述与代码上下文),并对 ChatGPT(gpt-3.5-turbo)进行查询。
- 通过 Java 解析器和测试执行,使用句法、编译和执行检查来评估正确性;对错误类型进行分类。
- 通过 Jacoco 使用语句覆盖和分支覆盖以及断言数量来评估充分性。
- 进行用户研究,比较手写测试,以评估可读性和可用性,面向在职 Java 开发者。
- 开发 ChatTester,包含面向意图的初始测试生成器与迭代的验证与修正改造器以提高测试正确性。
实验结果
研究问题
- RQ1RQ1: ChatGPT 生成的测试在语法、编译、执行方面有多正确,常见错误类型是什么?
- RQ2RQ2: ChatGPT 生成的测试在覆盖率与断言方面是否充分?
- RQ3RQ3: ChatGPT 生成的测试相较于人工测试的可读性如何?
- RQ4RQ4: 开发者是否会直接在真实项目中使用 ChatGPT 生成的测试?
- RQ5RQ5: ChatTester 是否在正确性上优于默认的 ChatGPT,其组件的贡献是什么?
主要发现
| Technique | Syntactic Correctness (%) | Compilation Correctness (%) | Execution Correctness (Passing) (%) |
|---|---|---|---|
| ChatGPT | 100.0 | 42.1 | 24.8 |
| AthenaTest | 54.8 | 18.8 | 14.4 |
| Evosuite | 100.0 | 66.8 | 59.7 |
- ChatGPT 生成的测试在语法正确性方面很高,但由于编译和断言错误,整体执行成功率较低。
- 只有 24.8% 的 ChatGPT 测试通过执行,而 57.9% 遇到编译错误,17.3% 因断言不正确而在执行阶段失败。
- 在通过执行的测试中,ChatGPT 在语句覆盖率方面达到 82.3%,分支覆盖率为 65.6%,可读性和可用性与人工测试相当。
- ChatGPT 生成的测试在断言数量和总体结构上与人工测试相似,若正确性问题得到解决,将具有实际潜力。
- AthenaTest 与 Evosuite 在正确性和覆盖率方面低于 ChatGPT,而 Evosuite 由于后生成筛选,在编译和执行率上表现更高。
- ChatTester——由面向意图的初始测试生成器和迭代的验证修正改造器组成——比默认的 ChatGPT 多产出 34.3% 的可编译测试和多出 18.7% 的具有正确断言的测试。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。