After struggling with an illogical UI I finally figured out how to add unit tests to an existing project in XCode 4.2.

Open your project.

Goto File -> New -> New Target

Select iOS Cocoa Touch bundle.

Give it a name like ‘LogicTests’

Then click on your blue app area in your navigation.

Then click ‘Build Phases’ and expand the Target Dependency area. See how it’s blank?

That’s a problem. We need to add your application here so your unit test target is linked to your app.

So go ahead and click the ‘+’ sign and add your application.

Now you should be able to run your unit tests (by holding down the build icon until it turns to a test icon).

When you do you’ll see a configure app for testing window pop-up.

Just click ‘Edit Scheme…’ hit the plus sign (making sure the Test/Debug panel is highlighted) and add your unit test scheme as shown below.

Now you can hit that test button and all your unit tests should run.

There’s a gotcha

There’s one more gotcha you need to be aware of. As of XCode 4.2 when you add a new test class

You will get a screen that looks like this:

The defaults here are wrong. The MyApp shouldn’t be selected. We want the unit test target ‘Logic Tests’ to be checked.

If you accept these, when you go to run your tests you’ll get an error like:

<SenTestingKit/SenTestingKit.h> file not found.

This occured because the wizard default added your new test class to your main project and not your test target.

You can manually fix this by going into the build phase of your project and adding your test file to your test target and removing it from your main application target under the ‘Compile Sources’ tab.

Or you can just create the class from scratch and select your test project and not the application one.

Hope this helps. Happy testing!