arXiv:2608.16742cs.SEcs.AI2026-08

让AI写代码前先写测试,通过反复迭代提升正确率。

TDD-Agent: Test-Driven Reasoning for Code Generation

论文配图:TDD-Agent: Test-Driven Reasoning for Code Generation
图 1 · 摘自论文原文
  • 先生成可运行的测试用例,再根据执行反馈迭代优化代码和测试。
  • 在RepoEval上表现优于基线,代码通过率、覆盖率和突变测试得分更高。
  • 适合需要高可靠性的自动化编程任务,如开源项目级代码生成。

大型语言模型在代码生成方面取得了显著进展,但在复杂、仓库级别的任务中保证正确性仍具挑战。现有方法常将生成的测试作为静态事后验证,难以指导实现,且当测试不完整或错误时可能产生误导性反馈。本文提出TDD-Agent,将测试驱动开发范式应用于代码生成。TDD-Agent首先引导模型生成可执行测试,促使模型在实现前明确预期行为,随后基于执行反馈对代码和测试进行迭代双轨优化。我们在LiveCodeBench上通过TDD-prompt变体验证了测试优先推理的有效性,结果持续优于基于推理的提示基线。进一步在仓库级基准RepoEval上评估完整TDD-Agent框架,结果一致超越检索型与代理型基线。额外分析表明,迭代优化不仅提升代码正确性,也增强测试有效性,带来更高的通过率、覆盖率和突变测试得分,说明测试可作为动态演进的推理载体而非固定验证器。源代码已公开于https://anonymous.4open.science/r/TDD-Agent-Framework-6370/。

原文摘要 · Abstract (English)

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/.

代码生成测试驱动迭代优化

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