Wednesday, July 9, 2008

Test Driven Development: A Short Story

Last Sunday, I scheduled myself to prepare for my next training assignment which is mostly related to Test Driven Development (TDD) and Unit Testing. I know how to do it but teaching it and making the participants appreciate TDD is a whole different thing for me. After browsing through the slides for the nth time, nothing came to me as to how I would attack this topic. Thankfully, Franz is at the office as well. He gave me something to read from The Craftsman. Its just a short story but gave a clear approach on how to teach TDD.


Come Tuesday that week, I was so excited to try out the approach I've learned in the story. I started by explaining the development approach that we will be learning for that day. I asked one student to be the typist and reminded that everybody is expected to share their thoughts on the application.


We used the same problem used in The Craftsman, they are to get the prime factors of a certain number. So we started writing their very first test. The test scenario is called testTwo which checks if the length of the array is correct and the value of the array is indeed 2. Of course their test failed after running it, so I asked them to write the simplest code that would pass the test. After writing down their solution, I was surprised it’s the same solution in the story that I was simulating. So I just continued by asking them to write the next test which would get the prime factors of 3. At this point, they're already making fun of me by saying "Oh we'll be testing the whole day, we'll be writing test for 2-1000!" What made me freak out is that their reactions and solutions are very similar to the story. The main deviation would be the additional/minor information I added during the process such as best practices in testing and seeing it LIVE! I never thought that the story predicted the events in such accurate points.


At the end of the exercise, the participants have completed the events in the story. They enjoyed the interactions that they had during the exercise and were really happy with what they just learned, AND that System.out.println() is not the only testing tool around.