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.
這里最難的是第一點(diǎn)的第二句,"只有正確的程序才能通過(guò)測(cè)試"。 這幾乎是不可能的,即使可能,所耗的時(shí)間也不亞于編寫代碼的時(shí)間. 第四點(diǎn)的提法更有問(wèn)題,程序員在編程實(shí)現(xiàn)功能的時(shí)候應(yīng)該把注意力集中在所實(shí)現(xiàn)的代碼,而不是測(cè)試上。
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.