arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2502.01619cs.SEcs.AIcs.CLcs.LG
收藏

学习生成用于自动化调试的单元测试

Learning to Generate Unit Tests for Automated Debugging

University of North Carolina at Chapel Hill(北卡罗来纳大学教堂山分校)

Archiki Prasad, Elias Stengel-Eskin, Justin Chih-Yao Chen, Zaid Khan, Mohit Bansal

AI总结:

本文提出 UTGen 和 UTDebug,通过生成能揭示错误且包含正确输出的单元测试来提升 LLM 自动化调试效果,并在多个基准上显著超越现有方法。

摘要:

单元测试(UTs)在评估代码正确性以及为大型语言模型(LLMs)提供反馈方面发挥着重要作用,这推动了自动化测试生成的研究。然而,我们发现了一个权衡:在给定有缺陷代码时,生成能够揭示错误的单元测试输入与在没有黄金解决方案的情况下正确预测单元测试输出之间存在矛盾。为了解决这一权衡,我们提出了 UTGen,它教导 LLMs 基于任务描述生成既能揭示错误的单元测试输入,又能给出正确期望输出。由于模型生成的测试可能提供噪声信号(例如来自错误预测的输出),我们提出了 UTDebug,它(i)通过测试时计算扩展 UTGen 以改进单元测试输出预测,并且(ii)基于多个生成的单元测试验证并回溯编辑以避免过拟合,从而帮助 LLMs 有效调试。我们表明,基于衡量同时包含揭示错误的单元测试输入和正确单元测试输出的指标,UTGen 比其他基于 LLM 的基线方法高出 7.59%。当与 UTDebug 一起使用时,我们发现来自 UTGen 单元测试的反馈使 Qwen2.5 32B 在 HumanEvalFix 以及我们自己构建的更难调试子集 MBPP+ 上的 pass@1 准确率分别比其他基于 LLM 的单元测试生成基线提高了超过 3.17% 和 12.35%。此外,我们观察到来自基于 Qwen2.5 32B 的 UTGen 模型的反馈可以将 GPT-4o 等前沿 LLMs 的调试效果提升 13.8%。最后,我们证明 UTGen 是更好的代码正确性判断器,在使用 Qwen2.5 7B 进行 best-of-10 采样的 HumanEval+ 上,比最先进的训练型 8B 奖励模型高出 4.43%。

英文摘要:

Unit tests (UTs) play an instrumental role in assessing code correctness as well as providing feedback to large language models (LLMs), motivating automated test generation. However, we uncover a trade-off between generating unit test inputs that reveal errors when given a faulty code and correctly predicting the unit test output without access to the gold solution. To address this trade-off, we propose UTGen, which teaches LLMs to generate unit test inputs that reveal errors along with their correct expected outputs based on task descriptions. Since model-generated tests can provide noisy signals (e.g., from incorrectly predicted outputs), we propose UTDebug that (i) scales UTGen via test-time compute to improve UT output prediction, and (ii) validates and backtracks edits based on multiple generated UTs to avoid overfitting, and helps LLMs debug effectively. We show that UTGen outperforms other LLM-based baselines by 7.59% based on a metric measuring the presence of both error-revealing UT inputs and correct UT outputs. When used with UTDebug, we find that feedback from UTGen's unit tests improves pass@1 accuracy of Qwen2.5 32B on HumanEvalFix and our own harder debugging split of MBPP+ by over 3.17% and 12.35% (respectively) over other LLM-based UT generation baselines. Moreover, we observe that feedback from Qwen2.5 32B-based UTGen model can enhance debugging with frontier LLMs like GPT-4o by 13.8%. Lastly, we demonstrate that UTGen is a better judge for code correctness, outperforming a state-of-the-art trained 8B reward model by 4.43% on HumanEval+ with best-of-10 sampling using Qwen2.5 7B.

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

↑