201710 Let's Talk About TDD - xiaoxianfaye/Courses GitHub Wiki
- 0 About TDD
- 1 Problems We Currently Face
- 2 Definition of TDD
- 3 Conventional Development vs. TDD Development
- 4 Three Standard Steps of TDD
- 5 F.I.R.S.T Principles of TDD
- 6 A.A.A Three Phases of a Test
- 7 Refactoring
- 8 Tools of TDD
- Problems We Currently Face
- Definition of TDD
- Conventional Development vs. TDD Development
- Three Standard Steps of TDD
- F.I.R.S.T Principles of TDD
- A.A.A Three Phases of a Test
- Refactoring
- Tools of TDD
- 糟糕的代码质量
- 不理解问题、不了解问题领域
- 不清楚问题的范围和边界
- 不动脑筋、缺乏思考:Ctrl + C、Ctrl + V
- 缺乏设计、过度设计
- 代码缺乏层次
- 重复代码
- Bug层出不穷
- 效率低下
- 枯燥乏味、没有信心、找不到乐趣
写代码只为修复失败了的测试
- Test-Driven Development (TDD) 测试驱动开发
- 坚持测试先行
- 从使用者的角度考虑设计
- 一种开发和设计技术,小步、增量式地构建系统
- 敏捷开发方法的核心实践
- 新增一个测试用例
- 添加恰好足够的代码使所有(含新增)测试用例快速通过
- 重构
小步
TDD需遵循F.I.R.S.T原则,《Clean Code》Written by Robert C. Martin。
快速:测试应该能快速运行。测试运行缓慢,你就不会想要频繁地运行它。如果你不频繁地运行测试,就不能尽早发现问题,也无法轻易修正,从而也不能轻而易举地清理代码。最终,代码就会腐坏。
独立:测试应该相互独立。某个测试不应为下一个测试设定条件。你应该可以单独运行每个测试,及以任何顺序运行测试。当测试相互依赖时,头一个没通过就会导致一连串的测试失败,使问题诊断变得困难,隐藏了下级错误。
可重复:测试应当可以在任何环境中重复通过。当环境条件不具备时,你也会无法运行测试。
自检:测试应该有布尔值输出。无论是通过或失败,你不应该通过查看打印信息来确认测试是否通过,无法自动化。
及时:测试应及时编写。单元测试应该恰好在使其通过的产品代码之前编写。如果在编写产品代码之后编写测试,你会发现产品代码难以测试。你可能不会去设计可测试的代码。
可按照AAA三段式编写一个测试。
准备:为测试做准备工作。
执行:执行测试内容相关的代码。
断言:校验结果。
在不改变代码外在行为的前提下,对代码做出修改,以改进程序的内部结构。
- 在测试保证下,消除代码坏味道
- 一种有纪律、训练有素、有条不紊的代码整理方法
- 小步改善现有设计、使软件更容易被理解和修改
- 重构时不仅不能引入缺陷,也不能添加新功能
- Clean code matters more
xUnit
https://en.wikipedia.org/wiki/XUnit
xUnit is the collective name for several unit testing frameworks that derive their structure and functionality from Smalltalk's SUnit. SUnit, designed by Kent Beck in 1998, was written in a highly structured object-oriented style, which lent easily to contemporary languages such as Java and C#. Following its introduction in Smalltalk the framework was ported to Java by Kent Beck and Erich Gamma and gained wide popularity, eventually gaining ground in the majority of programming languages in current use. The names of many of these frameworks are a variation on "SUnit", usually replacing the "S" with the first letter (or letters) in the name of their intended language ("JUnit" for Java, "RUnit" for R etc.). These frameworks and their common architecture are collectively known as "xUnit".