arXiv:2604.03135cs.SEcs.AI2026-04

用AI自动生成测试并安全重构代码,提升开发效率与可靠性

AI-Assisted Unit Test Writing and Test-Driven Code Refactoring: A Case Study

  • 用AI批量生成单元测试,捕获系统行为
  • 实现关键模块78%分支覆盖率,数小时完成原需数周工作
  • 适合希望提速重构且重视代码质量的团队

许多软件系统始于原型或最小可行产品(MVP),注重快速交付和响应需求变化,而非长期可维护性。这种做法虽利于快速迭代,却导致代码难以修改,带来显著机会成本。本文通过案例研究,探索利用编码模型自动编写单元测试并进行受控重构的流程。通过测试验证模型提出的修改,研究了如何迭代生成测试以捕捉现有行为,并在开发者监督下实现模型辅助重构。分析了重构约束机制、两阶段中的错误与局限、人工干预必要性,以及模型存在的价值错位问题。实验表明,该方法在数小时内生成近1.6万行可靠单元测试,关键模块分支覆盖率最高达78%,大幅降低大规模重构中的回归风险。结果展示了软件工程正向数据驱动的实证科学演进,强调数据收集与约束机制对高效安全迭代的支持。

原文摘要 · Abstract (English)

Many software systems originate as prototypes or minimum viable products (MVPs), developed with an emphasis on delivery speed and responsiveness to changing requirements rather than long-term code maintainability. While effective for rapid delivery, this approach can result in codebases that are difficult to modify, presenting a significant opportunity cost in the era of AI-assisted or even AI-led programming. In this paper, we present a case study of using coding models for automated unit test generation and subsequent safe refactoring, with proposed code changes validated by passing tests. The study examines best practices for iteratively generating tests to capture existing system behavior, followed by model-assisted refactoring under developer supervision. We describe how this workflow constrained refactoring changes, the errors and limitations observed in both phases, the efficiency gains achieved, when manual intervention was necessary, and how we addressed the weak value misalignment we observed in models. Using this approach, we generated nearly 16,000 lines of reliable unit tests in hours rather than weeks, achieved up to 78\% branch coverage in critical modules, and significantly reduced regression risk during large-scale refactoring. These results illustrate software engineering's shift toward an empirical science, emphasizing data collection and constraining mechanisms that support fast, safe iteration.

AI编程测试生成代码重构自动化

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