arXiv:2508.21107cs.SEcs.AI2025-08中稿 · ICLR被引 10

用对抗强化学习训练模型生成更有效的单元测试

Learning to Generate Unit Test via Adversarial Reinforcement Learning

  • 双模型对抗训练:一个生成测试,一个生成代码,互相挑战
  • 相比监督微调,生成的测试能更好暴露代码缺陷
  • 在高质量测试生成上超越GPT-4.1,适合自动化测试研究者

单元测试是编程中的核心实践,用于系统评估人类开发者或大语言模型(LLMs)生成的程序。由于编写全面的单元测试存在挑战,已有研究使用LLM自动产生活动测试,但如何训练LLM生成高质量测试仍缺乏探索。本文提出UTRL框架,通过强化学习迭代训练两个LLM:测试生成器和代码生成器,形成对抗机制。测试生成器以最大化鉴别奖励为目标,提升其发现代码生成器解中缺陷的能力;代码生成器则以最大化代码奖励为目标,生成能通过测试生成器所产测试的解。实验表明,经UTRL训练的Qwen3-4B生成的单元测试质量优于在人工标注测试上监督微调的同一模型,且其代码评估结果更接近真实测试。此外,该模型在生成高质量测试方面超越了前沿模型GPT-4.1,验证了UTRL的有效性。

原文摘要 · Abstract (English)

Unit testing is a core practice in programming, enabling systematic evaluation of programs produced by human developers or large language models (LLMs). Given the challenges in writing comprehensive unit tests, LLMs have been employed to automate test generation, yet methods for training LLMs to produce high-quality tests remain underexplored. In this work, we propose UTRL, a novel reinforcement learning framework that trains an LLM to generate high-quality unit tests given a programming instruction. Our key idea is to iteratively train two LLMs, the unit test generator and the code generator, in an adversarial manner via reinforcement learning. The unit test generator is trained to maximize a discrimination reward, which reflects its ability to produce tests that expose faults in the code generator's solutions, and the code generator is trained to maximize a code reward, which reflects its ability to produce solutions that pass the unit tests generated by the test generator. In our experiments, we demonstrate that unit tests generated by Qwen3-4B trained via UTRL show higher quality compared to unit tests generated by the same model trained via supervised fine-tuning on human-written ground-truth unit tests, yielding code evaluations that more closely align with those induced by the ground-truth tests. Moreover, Qwen3-4B trained with UTRL outperforms frontier models such as GPT-4.1 in generating high-quality unit tests, highlighting the effectiveness of UTRL in training LLMs for this task.

单元测试强化学习大模型

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。