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

基于自动反馈的强化学习用于高质量单元测试生成

Reinforcement Learning from Automatic Feedback for High-Quality Unit Test Generation

Benjamin Steenhoek, Michele Tufano, Neel Sundaresan, Alexey Svyatkovskiy

AI总结:

针对 LLM 生成单元测试常含测试坏味道的问题,提出 RLSQM,用静态质量指标作强化学习奖励优化 Codex,显著提升测试质量并优于 GPT-4。

摘要:

软件测试是软件开发中一个至关重要但耗时的环节,近来,大型语言模型(LLM)在自动化测试用例生成方面受到欢迎。然而,由于 LLM 是基于大量开源代码训练的,它们生成的测试用例往往不符合最佳实践,甚至可能包含测试坏味道(反模式)。为解决这一问题,我们提出了 Reinforcement Learning from Static Quality Metrics(RLSQM,基于静态质量指标的强化学习),其中利用强化学习根据基于静态分析的质量指标生成高质量单元测试。首先,我们分析了 LLM 生成的测试,并表明 LLM 确实经常产生不良的测试坏味道——比例最高达37%。随后,我们实现了轻量级的、基于静态分析的奖励模型,并使用该奖励模型训练 LLM,以优化五项代码质量指标。实验结果表明,经强化学习优化的 Codex 模型始终能比基础 LLM 生成更高质量的测试用例,将质量指标最高提升23%,并生成接近100%语法正确的代码。尽管训练的是成本显著更低的 Codex 模型,RLSQM 在所有代码质量指标上仍优于 GPT-4。我们提供了关于如何可靠利用强化学习提升测试生成质量的见解,并表明 RLSQM 是迈向提高自动化软件测试整体效率和可靠性的重要一步。我们的数据可在 https://doi.org/10.6084/m9.figshare.25983166 获取。

英文摘要:

Software testing is a crucial but time-consuming aspect of software development, and recently, Large Language Models (LLMs) have gained popularity for automated test case generation. However, because LLMs are trained on vast amounts of open-source code, they often generate test cases that do not adhere to best practices and may even contain test smells (anti-patterns). To address this issue, we propose Reinforcement Learning from Static Quality Metrics (RLSQM), wherein we utilize Reinforcement Learning to generate high-quality unit tests based on static analysis-based quality metrics. First, we analyzed LLM-generated tests and show that LLMs frequently do generate undesirable test smells -- up to 37% of the time. Then, we implemented lightweight static analysis-based reward model and trained LLMs using this reward model to optimize for five code quality metrics. Our experimental results demonstrate that the RL-optimized Codex model consistently generated higher-quality test cases than the base LLM, improving quality metrics by up to 23%, and generated nearly 100% syntactically-correct code. RLSQM also outperformed GPT-4 on all code quality metrics, in spite of training a substantially cheaper Codex model. We provide insights into how reliably utilize RL to improve test generation quality and show that RLSQM is a significant step towards enhancing the overall efficiency and reliability of automated software testing. Our data are available at https://doi.org/10.6084/m9.figshare.25983166.

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

↑