Is iPhone Unit Testing Possible?

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:

  1. Is iPhone Unit Testing Possible?
  2. How to Create an iPhone Project in Xcode That Can Run Unit Tests
  3. Getting Started Writing iPhone Unit Tests
  4. 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.

Apple says it is not possible. In the “Xcode Unit Testing Guide”, it says

“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:

  1. Application
  2. 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:

  1. Find all the unit tests in your Project
  2. Run all the unit tests
  3. 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.

2 Replies to “Is iPhone Unit Testing Possible?”

  1. Luis, you have written a very nice series that is needed in the iPhone dev and testing world. I encourage you to keep doing this for all our benefit.

    Ray

Comments are closed.