Now, you probably know that you should be writing unit tests for your applications. Good testing has a number of advantages. Obviously, it can help test bugs. It also helps with code maintenance. If you have a good suite of unit tests, you can make changes to your code without worrying about introducing new bugs (or re-introducing old bugs). Full Test Driven Development is also a highly effective method for writing new code.
Bottom line, unit tests are a lot like eating broccoli. We know we should do it. We know it is good for us. But, far too many of us don’t do enough.
Xcode has always supported unit testing through the OCUnit framework. However, support was somewhat primitive. Yes, we could write and run unit tests from within Xcode, but that’s about it. Testing was hard to automate, and I don’t even want to talk about the odd “logic test” versus “application test” segmentation.
Fortunately, Xcode 5 has greatly improved the support for unit testing. First, Xcode has moved to the new XCTest framework. While this largely appears to be a wrapper around the old OCUnit framework, I’m sure Apple plans to expand and enhance it in future releases.
We also get better integration between Xcode, our application, and our unit tests. Xcode provides additional controls, letting us run individual tests or our entire test suite with ease. Most importantly, though, we now have built-in support for continuous integration using bots and Mac OS X Server.
Mac OS X Server is an application that can run on any Mac running Mavericks. It provides a number of server services, like mail servers, web servers, file sharing, etc. It also provides a new Xcode server.
Set up a remote git repository for your code (this can either be on the machine running OS X Server, or on a separate server), then build and configure a bot to automatically run your test suites. We can then manage and monitor our bots either directly in Xcode or using a web browser.
Furthermore, Mac OS X Server is available for free to everyone who joins the Developer Program. So, there are no excuses. Even if you do all your development and testing on one machine, you can still set up continuous integration and automatic testing.
Having said all that, in my experience, most of the work in an iOS application focuses on developing the user interface. This code is often hard to test meaningfully. However, large portions of our code—and especially our model code—should be testable. There’s no excuse for not testing the parts that we can. And, there’s no excuse for not automating our tests.