The TDD cycle looks like this:
-
Write a test for the next bit of functionality you have in mind. The test should succeed only when the functionality has been implemented correctly.
-
Make the test compile by creating stubs for all the missing classes and methods referenced by the test.
-
Run the test. It should fail.
-
Implement just enough functionality to get the test to succeed.
-
Clean up the implementation as much as possible, typically by removing duplication.
这里最难的是第一点的第二句,"只有正确的程序才能通过测试"。 这几乎是不可能的,即使可能,所耗的时间也不亚于编写代码的时间. 第四点的提法更有问题,程序员在编程实现功能的时候应该把注意力集中在所实现的代码,而不是测试上。
The Test/Code Cycle in XP
- Write one test.
- Compile the test. It should fail, as you haven't implemented anything yet.
- Implement just enough to compile. (Refactor first if necessary.)
- Run the test and see it fail.
- Implement just enough to make the test pass.
- Run the test and see it pass.
- Refactor for clarity and "once and only once".
- Repeat from the top.