[论文解读] Large Language Models as Test Case Generators: Performance Evaluation and Enhancement
本论文评估大型语言模型(LLMs)作为测试用例生成器,并推出 TestChain,这是一个具备 Python 解释器的多代理框架,旨在提高生成测试用例的准确性和鲁棒性,特别是在较难的问题上。
Code generation with Large Language Models (LLMs) has been extensively studied and achieved remarkable progress. As a complementary aspect to code generation, test case generation is of crucial importance in ensuring the quality and reliability of code. However, using LLMs as test case generators has been much less explored. Current research along this line primarily focuses on enhancing code generation with assistance from test cases generated by LLMs, while the performance of LLMs in test case generation alone has not been comprehensively examined. To bridge this gap, we conduct extensive experiments to study how well LLMs can generate high-quality test cases. We find that as the problem difficulty increases, state-of-the-art LLMs struggle to generate correct test cases, largely due to their inherent limitations in computation and reasoning. To mitigate this issue, we further propose a multi-agent framework called \emph{TestChain} that decouples the generation of test inputs and test outputs. Notably, TestChain uses a ReAct format conversation chain for LLMs to interact with a Python interpreter in order to provide more accurate test outputs. Our results indicate that TestChain outperforms the baseline by a large margin. Particularly, in terms of the accuracy of test cases, TestChain using GPT-4 as the backbone achieves a 13.84\% improvement over the baseline on the LeetCode-hard dataset.
研究动机与目标
- 评估最先进的 LLMs 在不同难度数据集上生成函数级单元测试用例的能力。
- 确定限制测试用例正确性的主要错误类型(例如 Assertion Errors、Runtime Errors、Timeouts)。
- 提出并验证一个解耦的多代理架构(TestChain),它利用 Python 解释器来改进输入输出映射。
- 分析 0-shot 与 1-shot 提示的影响以及工具辅助推理在测试用例生成中的作用。
提出的方法
- 在 HumanEval-no-exp 和 LeetCode-no-exp 数据集上,对四个 LLMs(StarChat、CodeLlama、GPT-3.5、GPT-4)进行大规模实验。
- 使用准确率、行覆盖率,以及一个新的 Code-with-Bugs (CwB) 指标来评估 HumanEval 的测试用例。
- 分析错误类型(Assertion、Runtime、Timeout)以理解失效模式。
- 提出 TestChain,包含两个代理(Designer 用于输入、Calculator 用于输出)并通过 ReAct 风格交互提供 Python 解释器,以生成并验证测试用例。
- 将 TestChain 与基线 Test Agent(1-shot)以及一个没有 Python 解释器的修改版 TestChain 进行比较,以评估解耦和工具交互的价值。
- 在可用时报告 0-shot vs 1-shot 提示的结果。
实验结果
研究问题
- RQ1现在的 LLMs 能否在简单到困难的 Python 问题上为函数级单元测试生成高质量测试用例?
- RQ2LLMs 生成测试用例时的主导错误类型是什么,它们如何随问题难度而扩展?
- RQ3将测试输入生成与输出生成解耦是否能提高测试用例的准确性和强度?
- RQ4通过 ReAct 风格交互引入 Python 解释器是否显著提升生成测试用例的正确性?
- RQ5提示策略(0-shot 与 1-shot)如何与该任务的模型能力相互作用?
主要发现
| 模型 | 方法 | HumanEval-no-exp 准确率 (%) | HumanEval-no-exp 行覆盖率 (%) | HumanEval-no-exp CwB (%) | LeetCode-no-exp 准确率 (%) | LeetCode-no-exp 行覆盖率 (%) |
|---|---|---|---|---|---|---|
| GPT-3.5 | Test Agent (1-shot) | 74.02 | 74.69 | 74.15 | 38.97 | 73.66 |
| GPT-3.5 | TestChain | 80.85 | 77.53 | 80.80 | 48.72 | 80.23 |
| GPT-4 | Test Agent (1-shot) | 84.63 | 77.04 | 83.11 | 57.95 | 88.47 |
| GPT-4 | TestChain | 90.24 | 80.00 | 88.66 | 71.79 | 90.60 |
- LLMs 在简单问题上生成大量测试用例,但在像 LeetCode-hard 这样的困难问题上准确性显著下降。
- CwB 与行覆盖在不同模型上显示出不同的改进,难点在于准确性是困难问题的瓶颈。
- TestChain 在各项指标和数据集上始终优于 Test Agent(1-shot)基线。
- TestChain 使用 GPT-4 在 LeetCode-no-exp 上实现 71.79% 的准确性,相较基线(1-shot)的 57.95%。
- 使用带 Python 解释器的 Calculator 代理能减少 Assertion Errors,并提升正确性和覆盖率,相较于单发或无解释器的设置。
- 没有 Python 解释器的修改版 TestChain 不如完整的 TestChain 强,凸显工具辅助计算的关键作用。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。