Lessons Learned: Unit Testing iPhone Apps
This is the first part of a four part series on How to do iPhone Unit Testing. You could also call it Lessons Learned from Unit Testing iPhone Apps. I’ve learned a lot of lessons over the past year doing iPhone Consulting and I want to share them with everyone.
Here is the schedule for this series:
- Is iPhone Unit Testing Possible?
- How to Create an iPhone Project in Xcode That Can Run Unit Tests
- Getting Started Writing iPhone Unit Tests
- Pitfalls That You May Encounter when Running iPhone Unit Tests and How to Overcome Them
This is targeted at iPhone Developers, especially those who have done unit testing in other languages and frameworks.
A bit of backstory: My name is Luis de la Rosa and I am an iPhone Consultant. I have been making iPhone applications for clients the past year through my company Happy Apps LLC. I have also been developing Mac OS X apps and Rails apps for the past 3 1/2 years and in other languages for 14+ years now.
To start off, you might ask: Is iPhone Unit Testing even possible? I don’t see it as an option when creating a New Target in Xcode like I can with Mac OS X applications.
“iPhone OS Unit Testing Support: Unit tests are not supported for iPhone applications.”Â
But what this really means is that Unit Test Bundles, which are dynamic, are not allowed on iPhone because dynamic bundles of all kinds are not allowed on iPhone. So the normal way of adding Unit Tests to an Xcode project is not available to you. (iPhone projects are Xcode projects.)
However, there is a way to add unit tests to an iPhone project!
To understand why, you need to understand the two types of targets available to an iPhone project. These two are:
- Static Library
To put it another way: the normal Unit Test Bundle target for Mac OS X applications is not available because there are no Dynamic Bundle targets. A Static Library target cannot be executed and we need some sort of execution in order to run the tests. So we need to somehow use an Application target to run our unit tests.
The Application target will need to do the following things:
- Find all the unit tests in your Project
- Run all the unit tests
- Report the results of running the unit tests. This should include the number of successes and failures overall and also the results of each individual test.
You could do this from scratch, but fortunately there are already some diligent and ingenious engineers out there who have already done the work for you.
In tomorrow’s segment: How to create an iPhone Project in Xcode that can run unit tests.
If you found this helpful and/or interesting (hopefully both), please leave a comment.