The Three Rules of TDD.
TDD的三条规则
邓辉 译
Over the years I have come to describe Test Driven Development in terms of three simple rules. They are:
这些年来, 我喜欢用下面三条简单的规则来描述测试驱动开发:
- You are not allowed to write any production code unless it is to make a failing unit test pass.
1
、除非为了使一个失败的unit test通过,否则不允许编写任何产品代码
- You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
- You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
3
、只允许编写刚好能够使一个失败的unit test通过的产品代码
You must begin by writing a unit test for the functionality that you intend to write. But by rule 2,you can't write very much of that unit test. As soon as the unit test code fails to compile,or fails an assertion,you must stop and write production code. But by rule 3 you can only write the production code that makes the test compile or pass,and no more.