# 测试驱动开发

**测试驱动开发**（TDD）是一种敏捷软件开发实践，开发者先编写一个会失败的自动化单元测试，再编写满足该测试的生产代码，然后以短迭代反复循环。它建立在测试—编码的微型迭代循环之上，与“后测试”方法形成对照——后者通常先编码后测试，两者很少交替进行。<sup>[1](https://arxiv.org/abs/2011.11942)</sup> 这一实践由美国软件工程师、极限编程的创立者[肯特·贝克](https://www.edgepedia.org/zh/kent-beck)（Kent Beck）在其著作《测试驱动开发：实战与模式解析》（Test Driven Development: By Example，艾迪生-韦斯利出版社，2002年）中加以系统化。<sup>[2](https://www.pearson.com/en-us/subject-catalog/p/Beck-Test-Driven-Development-By-Example/P200000009421/9780321146533)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心规则 | 除非先写下一个会失败的自动化测试，否则不要写一行新的生产代码。<sup>[1](https://arxiv.org/abs/2011.11942)</sup> |
| 内层循环 | 红（写一个会失败的测试）、绿（让所有测试通过）、重构（在测试保持通过的前提下清理代码）。<sup>[3](https://gds-way.digital.cabinet-office.gov.uk/standards/test-driven-development.html)</sup> |
| 起源 | 由肯特·贝克在《测试驱动开发：实战与模式解析》（艾迪生-韦斯利出版社，2002年）中系统化，是极限编程的一部分。<sup>[2](https://www.pearson.com/en-us/subject-catalog/p/Beck-Test-Driven-Development-By-Example/P200000009421/9780321146533)</sup> |
| 实测效果 | 2013年一项涵盖27项研究的元分析发现，质量提升与生产率下降在工业研究中均大于学术研究。<sup>[4](https://ieeexplore.ieee.org/abstract/document/6197200)</sup> |
| 新手结果 | 在一组12项受控实验中，TDD新手采用迭代式后测试开发所达到的代码质量略高于采用TDD。<sup>[1](https://arxiv.org/abs/2011.11942)</sup> |
| 采用障碍 | 一项系统综述识别出七项限制因素，包括开发时间增加、TDD经验不足和遗留代码。<sup>[5](https://www.researchgate.net/publication/220719935_Factors_Limiting_Industrial_Adoption_of_Test_Driven_Development_A_Systematic_Review)</sup> |

## 红—绿—重构循环

TDD的内层循环通常被称为红—绿—重构，得名于测试套件在每个阶段的状态。<sup>[3](https://gds-way.digital.cabinet-office.gov.uk/standards/test-driven-development.html)</sup>

**红。** 开发者从一组期望行为中取出恰好一项，将其转化为具体、可运行的测试。该测试在运行时应当失败，并且以预期的方式失败；意外的失败可能表明存在功能缺口或实现错误。<sup>[3](https://gds-way.digital.cabinet-office.gov.uk/standards/test-driven-development.html)</sup> 在编写测试的同时，会创建应用程序API的最小骨架以使测试能够编译，重点在于测试本身的正确性与完整性。<sup>[6](https://agilealliance.org/someassemblyrequired_bettersoftware_jan2008/)</sup>

**绿。** 焦点转向系统代码，实现的量恰好足以让包括新测试在内的所有测试通过。此阶段会频繁运行测试，运行缓慢的测试会被标记出来留待重构阶段处理。<sup>[3](https://gds-way.digital.cabinet-office.gov.uk/standards/test-driven-development.html)</sup>

**重构。** 以通过的测试作为安全网，开发者通过消除重复、应用众所周知的模式来改进设计与实现，全程保持行为不变。在贝克的框架中，软件设计的大部分工作正是在这里发生的。<sup>[3](https://gds-way.digital.cabinet-office.gov.uk/standards/test-driven-development.html)</sup> 循环往复，直到行为列表被穷尽。<sup>[3](https://gds-way.digital.cabinet-office.gov.uk/standards/test-driven-development.html)</sup>

其所陈述的理由是：先测试后编码迫使开发者提前思考代码结构、限制所写的内容，并以小增量进行重构，从而应能使每项功能随着时间推移更不易出错。<sup>[7](https://arxiv.org/abs/1809.01828)</sup> 英国政府数字服务局（GDS）的指南补充说，测试先行的做法能对设计给出快速反馈：难以测试或依赖过多的代码，正是需要重构的信号。<sup>[3](https://gds-way.digital.cabinet-office.gov.uk/standards/test-driven-development.html)</sup>

## 实证证据说明了什么

关于TDD益处的研究记录并不一致，结果在很大程度上取决于比较是如何设置的。

**元分析。** 2013年一项对27项关于外部代码质量与生产率的研究所做的元分析发现，与TDD相关的质量提升和生产率下降，在工业研究中都远大于学术研究。<sup>[4](https://ieeexplore.ieee.org/abstract/document/6197200)</sup>

**受控实验。** 一组在学术界和工业界开展的12项受控实验，其结果经元分析汇总后发现：TDD新手采用迭代式后测试开发（ITL）——即与TDD相反的顺序——所达到的代码质量略高于采用TDD本身。所开发的任务在很大程度上决定了质量，而编程环境以及两种方法的施用顺序似乎并不影响质量。总体而言，从业者达到的质量高于学生，但其采用TDD时的表现下降幅度大于学生，作者推测这可能反映了更强的变革抵触或更高的机会成本。<sup>[1](https://arxiv.org/abs/2011.11942)</sup> 一项相关的选择性元分析比较了TDD与迭代式后测试开发的受控实验，未发现两者在生产率、代码复杂度、分支覆盖率、通过的验收测试或变异分数上存在统计显著差异。<sup>[8](https://www.sciencedirect.com/science/article/abs/pii/S0950584911000346)</sup>

**究竟是什么带来了益处。** 2017年一项对TDD过程的剖析发现，测试的顺序——无论是TDD式的先行还是ITL式的后行——似乎并不影响外部质量。作者提出，归功于TDD的益处可能来自短的开发周期，而非测试先行的顺序，这也能解释为何TDD相对于没有短周期的瀑布模型的优势，比相对于同样具有短周期的ITL的优势更大。<sup>[9](https://research.monash.edu/en/publications/a-dissection-of-the-test-driven-development-process-does-it-reall/)</sup>

**工业案例。** 在北欧游戏娱乐公司Paf的一项实验中，TDD在外部质量上优于公司既有实践和ITL，但差异小到研究者不建议立即采用；他们建议将部分开发者转为使用TDD，待其积累经验后再重做实验。<sup>[7](https://arxiv.org/abs/1809.01828)</sup> 2017年一项对GitHub仓库按TDD实践严格程度分类的比较分析发现，这些仓库在提交速度、引用缺陷的提交、议题、持续集成的使用或拉取请求方面，与匹配的对照仓库几乎没有显著差异，并指出Java的TDD项目相对少见。<sup>[10](https://softwareprocess.es/homepage/papers/2017-borle2017emse-tdd/)</sup>

案例研究往往报告大幅收益，而受控实验报告的差异却很小甚至为负，这一差距被归因于研究时长、参与者对TDD的不熟悉以及对照方法的选择。案例研究通常持续数月乃至数年，使开发者有时间将这一实践内化，而实验只持续数天或数周。<sup>[1](https://arxiv.org/abs/2011.11942)</sup>

## 采用障碍

通过对实证研究的系统综述，研究者发现了阻碍TDD在工业界采用的七项障碍：开发时间更长、TDD经验或技能不足、缺少预先进行的设计、与领域和工具相关的困难、开发者编写测试用例的能力薄弱、对TDD规程的执行不力，以及[遗留代码](https://www.edgepedia.org/zh/legacy-code)。遗留代码是尤为突出的障碍，因为原初形式的TDD假定代码是从零开始开发的，并未涉及如何处理既有系统，而这些系统往往缺乏长期TDD所产生的单元级回归测试套件。<sup>[5](https://www.researchgate.net/publication/220719935_Factors_Limiting_Industrial_Adoption_of_Test_Driven_Development_A_Systematic_Review)</sup> 同一综述还报告，在一项涵盖22种测试相关实践的工业调查中，TDD在“不满意”一项上得分最高，意味着受访者希望使用它的程度显著高于他们实际使用的程度。<sup>[5](https://www.researchgate.net/publication/220719935_Factors_Limiting_Industrial_Adoption_of_Test_Driven_Development_A_Systematic_Review)</sup>

## 实践中的适用范围与局限

当反馈循环很快时，TDD效果最佳。GDS的指南认为它对单元测试（应在亚秒级时间内运行）和组件集成测试非常有效，但对反馈循环较长的过程（如跨多个组件的特性验收测试）效果较差。随着系统变得更为复杂，建立在快速测试基础上的探索性测试有助于发现单元测试和集成测试无法枚举的行为。<sup>[3](https://gds-way.digital.cabinet-office.gov.uk/standards/test-driven-development.html)</sup>

这一实践也成为公开辩论的主题。2014年，在[Ruby on Rails](https://www.edgepedia.org/zh/ruby-on-rails)的[创造者戴维·海涅迈尔·汉森](https://www.edgepedia.org/zh/david-heinemeier-hansson)（David Heinemeier Hansson）在博客文章中宣称“TDD已死”之后，以重构方面的工作而闻名的英国软件开发者马丁·福勒（Martin Fowler）主持了一系列与汉森和肯特·贝克的录制对谈。讨论将TDD与一般意义上的自测试代码区分开来，考察了大量使用模拟对象所造成的测试引发的设计损伤，并权衡了在决定测试量时频率、保真度、开销和寿命之间的取舍。<sup>[11](https://martinfowler.com/articles/is-tdd-dead/)</sup>

从业者的兴趣持续受到追踪：State of TDD调查是一项年度行业活动，其2024年版以47题的匿名调查形式在Google Forms上进行，开放时间为2024年10月至11月，涵盖各行业和各经验层次的采用情况、挑战与感知到的益处。<sup>[12](https://thestateoftdd.org/results/2024)</sup>

## References

1. [A Family of Experiments on Test-Driven Development (Santos et al., arXiv, 2020)](https://arxiv.org/abs/2011.11942)
2. [Test Driven Development: By Example, Kent Beck, Addison-Wesley (Pearson catalog)](https://www.pearson.com/en-us/subject-catalog/p/Beck-Test-Driven-Development-By-Example/P200000009421/9780321146533)
3. [Test-driven development (TDD), The GDS Way, UK Government Digital Service](https://gds-way.digital.cabinet-office.gov.uk/standards/test-driven-development.html)
4. [The Effects of Test-Driven Development on External Quality and Productivity: A Meta-Analysis, IEEE Transactions on Software Engineering](https://ieeexplore.ieee.org/abstract/document/6197200)
5. [Factors Limiting Industrial Adoption of Test Driven Development: A Systematic Review (Causevic et al., 2011)](https://www.researchgate.net/publication/220719935_Factors_Limiting_Industrial_Adoption_of_Test_Driven_Development_A_Systematic_Review)
6. [Red-Green-Refactor, Agile Alliance / Better Software (2008)](https://agilealliance.org/someassemblyrequired_bettersoftware_jan2008/)
7. [Improving Development Practices through Experimentation: an Industrial TDD Case (Santos et al., arXiv, 2018)](https://arxiv.org/abs/1809.01828)
8. [Impact of test-driven development on productivity, code and tests: A controlled experiment, Information and Software Technology](https://www.sciencedirect.com/science/article/abs/pii/S0950584911000346)
9. [A Dissection of the Test-Driven Development Process: Does It Really Matter to Test-First or to Test-Last? (Fucci et al., 2017)](https://research.monash.edu/en/publications/a-dissection-of-the-test-driven-development-process-does-it-reall/)
10. [Analyzing the effects of test driven development in GitHub (Borle et al., Empirical Software Engineering, 2017)](https://softwareprocess.es/homepage/papers/2017-borle2017emse-tdd/)
11. [Is TDD Dead?, Martin Fowler (2014)](https://martinfowler.com/articles/is-tdd-dead/)
12. [The State of TDD, 2024 Results](https://thestateoftdd.org/results/2024)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Development methodologies and project management

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/test-driven-development

English original: https://www.edgepedia.org/test-driven-development
