Kent Beck在他的TDD一书讲到有关TDD的“节奏”的问题:
The rhythm of test-driven development
1. Quickly add a test
2. Run all tests and see the new one fail
3. Make a little change
4. Run all tests and see them all succeed
5. Refactor to remove duplication
很喜欢rhythm这个词。实际上,如Kent所述,编写Test Case有如跳“韵律操”,节奏快慢全然在乎于自己的掌控。而对于节奏的把握,恰是因人而异的,即使同一个人于不同时段,亦有不同的表现。以往曾听人抱怨于TDD的“琐碎”:一个Money Example,就这么点内容,要分作这么多步序,要构筑一个大系统岂不累死人了。并且,这种夸张的“微观”行为难道不会影响实践者的“大局关”吗?难道不会使其偏题于千里之外吗?殊不知,Kent作为教练,在传授TDD技艺的时候,自然是要循循善诱的讲解分解动作的。但是作为实践者的我们,并非要拘泥于老师所教的“节奏”,所谓授人以渔,这才是老师的真正本意。何况Kent在书中不止一次的重申了这一点,看看下面这些散布于TDD中的零星陈述,是不是觉得软件的实现过程也很具“韵律”呢?
When everything is going smoothly and you know what to type, use Obvious Implementation—type in the real implementation. As soon as you get an unexpected red bar, back up, shift to Fake It—return a constant and gradually replace constants with variables until you have the real code. When confidence is back, go back to obvious implementations.
This is the kind of tuning you will be doing constantly with TDD. Are the teenytiny steps feeling restrictive? Take bigger steps. Are you feeling a little unsure? Take smaller steps. TDD is a steering process—a little this way, a little that way. There is no right step size, now and forever.
It is not necessary to work in such tiny steps as these. Once you’ve mastered TDD, you will be able to work in much bigger leaps of functionality between test cases. However, to master TDD you need to be able to work in such tiny steps when they are called for.
You will likely end up with about the same number of lines of test code as model code when TDDing. For TDD to make economic sense, either you will have to be able to write twice as many lines per day as before, or write half as many lines for the same functionality. You’ll have to measure and see what effect TDD has on your own practice. Be sure to factor debugging, integrating, and explaining time into your metrics, though.