Test driven development: Does it work for iPhone apps?
Short answer: Sure!
Longer answer: eBay Instant Sale went live in the App Store two days ago. I can’t share the source code with you, of course. But here’s the unit test coverage:
Preventing bugs isn’t a TDD goal. It’s more like a side effect.
It was written almost entirely using TDD. Sometimes tests weren’t written first (especially for code by a new engineer I couldn’t mentor because I was away). But test first or test last, they got written.
“That’s fine,” you may say, “but what benefit did they have?” If you’ve never done TDD, you haven’t felt the empowerment it brings. Read the following statements twice:
Writing tests before writing production code shaped the interfaces, leading to better design.
High test coverage let me refactor when the code needed it, leading to cleaner implementation.
Did you read the statements again? Design + implementation. Think about it.
A bit more about that 92%:
- I leave third-party code out of the coverage calculation. This goes for open source (such as JSON parsing) and internal code used in other eBay apps. You don’t need to write unit tests for code you’re not going to change.
- At one point, the coverage was as high as 95%. I felt quite good about that. But when I met Jens Nerup in person and shared this number, he asked me a challenging question: “What about the remaining 5%?” 100% coverage may not be attainable, but don’t stop at an arbitrary number.
- Having high test coverage doesn’t mean bugs weren’t discovered. There weren’t very many, but preventing bugs isn’t a TDD goal. It’s more like a side effect. Again, the goal is shaping interfaces and enabling bold refactoring. Design + implementation.
So iPhone Test Driven Development isn’t a pipe dream. It’s my reality, and it can become yours. Stick around to learn how; there will be more to come if you subscribe to this blog.
If you’ve used TDD to write your apps, don’t be shy! Leave a comment below and tell us about it.
See also: Xcode TDD 101 with the Bowling Game Kata