How do you learn Test Driven Development? I could explain the principles and practices of Xcode TDD, but the question that comes back is, “But what do I actually do in Xcode?”
That’s what the Bowling Game Kata is for. (Download PDF.)
What’s a code kata?
“Kata” is a Japanese martial arts term for choreographed patterns of movement. Also called “forms,” both students and masters practice these detailed patterns over and over, so that the movements can come without thought.
A “code kata” applies this idea to coding. Some use the term to refer to coding puzzles (how would you code this or that) but let’s be faithful to the martial arts metaphor. A code kata is a set of moves, meant to be memorized and practiced until they can flow effortlessly. “Uncle Bob” Martin designed the Bowling Game Kata to impart the moves of test driven development. I have taken his presentation and created a version showing these moves in Objective-C with Xcode 4.
Specific moves for Xcode TDD 101
I’m not there with you to walk through my “Xcode TDD 101″ presentation, so let me call out some specific parts:
- Slide 11: Go to my Resources page and download “Unit Test Template for Xcode 4″. When properly installed, you should see the big green checkmark as shown on my post “Warning: Don’t Use Apple’s Unit Test Template.”
- Slide 12: To add a new file, drag the big green checkmark from the File Template Library into your project navigator.
- Slide 14: You can set up your Xcode display to mimic the slide. Click the test file so it shows. Option-click the .h file so it shows on the right. Shift-option-click the .m file and double-click the “+” in the bottom right.
- Slide 25: Select the two circled lines, then use the contextual menu to Refactor → Extract.
- Slide 42: Rename loop variable “i” in one shot by selecting its first appearance and doing Edit All in Scope. This is available through the menus, or in a little pop-up menu that appears when you hover over a selection, as shown here:
Going through the kata
If you’re doing this on your own, I’d recommend going through the entire kata, once. Then go back, and break it down:
- Begin / Verify that tests run.
- The first test.
- The second test.
- The third test.
- The fourth test.
- The fifth test.
Memorize each section in turn. For example, work on “Begin / Verify that test run” and get it down before you move on to “The first test.”
Time-frame: Leading my coworkers through the first three tests took an hour and a half. I left the last two tests as homework.
Ready? Download the Bowling Game Kata for Objective-C and start practicing Xcode TDD!
(And if you subscribe to Quality Coding, you’ll receive a free set of Xcode 4 code snippets that will give your unit testing moves even more “flow!”)
Finally, for folks in Silicon Valley: If you’d like me to teach “Xcode TDD 101″ at a meetup or your workplace, get in touch with me.
Enjoy. Let me know how it goes.