TDD-Agent: Test-Driven Reasoning for Code Generation

📄 arXiv: 2608.16742v1 📥 PDF

作者: Hongyue Yu, Kefan Li, Jiakun Li, Hongzheng Chai, Yuan Yuan, Rui He, Junyi Wei

分类: cs.SE, cs.AI

发布日期: 2026-08-17


💡 一句话要点

提出TDD-Agent以解决代码生成中的正确性问题

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 代码生成 测试驱动开发 大型语言模型 迭代优化 软件工程

📋 核心要点

  1. 现有方法在复杂代码生成任务中难以确保代码的正确性,且测试的静态验证方式存在局限性。
  2. TDD-Agent通过先生成可执行测试,明确预期行为,然后进行迭代精炼,来实现测试驱动的代码生成。
  3. 在RepoEval基准测试中,TDD-Agent表现优于其他基线方法,且迭代精炼提高了代码和测试的有效性。

📝 摘要(中文)

大型语言模型(LLMs)在代码生成方面取得了显著进展,但在复杂的代码库级任务中确保正确性仍然具有挑战性。现有方法通常将生成的测试作为静态的事后验证工具,这限制了其指导实现的能力,并可能在测试本身不完整或不正确时引入误导性反馈。本文提出了TDD-Agent,将测试驱动开发范式应用于代码生成。TDD-Agent首先提示模型生成可执行的测试,促使其在实现之前明确预期行为,然后利用执行反馈对生成的代码和测试进行迭代双轨精炼。实验结果表明,TDD-Agent在RepoEval基准测试中持续优于检索基础和基于代理的方法,且迭代精炼不仅提高了代码的正确性,还增强了生成测试的有效性。

🔬 方法详解

问题定义:本文旨在解决在复杂代码生成任务中,现有方法依赖静态测试验证导致的代码正确性不足的问题。现有方法往往在测试不完整或不正确时,给出误导性反馈。

核心思路:TDD-Agent的核心思路是将测试驱动开发(TDD)范式应用于代码生成,通过先生成测试来明确预期行为,从而引导代码的实现,并通过执行反馈进行迭代优化。

技术框架:TDD-Agent的整体架构包括两个主要阶段:首先生成可执行的测试,然后在执行反馈的基础上对生成的代码和测试进行双轨迭代精炼。

关键创新:TDD-Agent的创新在于将测试作为动态演进的推理工具,而非静态验证器,显著提升了代码生成的正确性和测试的有效性。

关键设计:在设计中,TDD-Agent使用了特定的提示变体(TDD-prompt)来隔离测试优先推理的效果,并在LiveCodeBench上进行验证,确保其在不同基线上的一致性提升。

🖼️ 关键图片

fig_0
fig_1

📊 实验亮点

在RepoEval基准测试中,TDD-Agent的表现持续优于检索基础和基于代理的方法,迭代精炼不仅提高了代码的正确性,还提升了生成测试的通过率、覆盖率和变异分数,显示出测试作为演进推理工具的潜力。

🎯 应用场景

TDD-Agent的研究成果在软件开发、自动化测试和代码生成等领域具有广泛的应用潜力。通过提高代码生成的正确性和测试的有效性,能够显著提升软件开发的效率和质量,未来可能对开发工具和平台产生深远影响。

📄 摘要(原文)

Large Language Models (LLMs) have achieved remarkable progress in code generation, yet ensuring correctness in complex, repository-level tasks remains challenging. Existing approaches often use generated tests as static post-hoc validators, which limits their ability to guide implementation and may introduce misleading feedback when the tests themselves are incomplete or incorrect. In this paper, we introduce TDD-Agent, which operationalizes the test-driven development paradigm for code generation. TDD-Agent first prompts the model to generate executable tests, encouraging it to clarify expected behaviors before implementation, and then performs iterative dual-track refinement over both the generated code and tests using execution feedback. We first isolate the effect of test-first reasoning through a prompt variant TDD-prompt on LiveCodeBench, where it consistently improves upon reasoning-based prompting baselines. Building on this finding, we evaluate the full TDD-Agent framework on RepoEval, a repository-level benchmark, and show that it consistently outperforms retrieval-based and agent-based baselines. Additional analyses show that iterative refinement improves not only code correctness but also the effectiveness of the generated tests, yielding higher pass rates, coverage, and mutation scores, suggesting that tests can serve as evolving reasoning artifacts rather than fixed validators. Our source code is available at https://anonymous.4open.science/r/TDD-Agent-Framework-6370/.