Testing? For iPhone? Mooooo….*

I wrote a blog post about Cedar way back in the day. Now, Xcode ships with OCUnit.

I’ll go through a basic way of adding tests to an existing project, as that’s a very common task, and not very well documented thus far. Props to the following blog posts- I’m consolidating their advice basically in a single one, with screenshots.

Why do tests? Well, it’s a great way to work. If this is new to you and you’re more interested, check out tons of resources referring to Test Driven Development (TDD). Of course, if you read this from a Google search, you already know what that means.

1. Add New Target

1. In Xcode 4 (.3.3, for me), add a Target. Took me forever to find this. It’s the “+” circle at the bottom of the Project pane:

2. Set to Cocoa Touch Unit Testing Bundle

2. Change Target Build Phases & Settings

Click “Build Phases”, expand “Target Dependencies” and add your App.

Click “Build Settings”, and add:
Under “Links”, “bundle Loader”, “build/Debug-iphoneos/[yourapp].app/[yourapp]

Under “Unit Testing”, “test host” put in “$(BUNDLE_LOADER)” (will populate with bundle loader).
3. Change App’s Bundle Settings

Set “Symbols Hidden by Default” to “NO” – frankly, this is usually already set to No.

4. Test to Run!

So let’s run this, even though the sample test sends a failure. Still, it’s good to see that in action right off.

Now, if you’ve installed this into an already made project, you will select the target in the “manage scheme” area, then select Product/Test from the menu.

One distinction- if you’ve started a project with OCUnit “baked in”, then you don’t have to select the scheme/target in the upper left, simply use Product/Test from the menu.

5. Check Out Those Errors

Unlike the lovely test frameworks in rails, and some other languages, OCUnit prints out boring black and white console log errors: so have your console up and running when you Product/Test. Here’s some live test results!

* Testing your Bay Area cred… this title is from the ad that ran 50s… 90s in the SF Bay Area for Berkeley Farms, that had a very catchy tagline: “Farms? In Berkeley? Moooo…” Several radio spots can be heard on their site now, and you can check out some old photos of this neat Berkeley dairy.