What frameworks exist to unit test Objective-C code? I would like a framework that integrates nicely with Xcode.

+6  A: 

Check out OCUnit. Apple's developer network has a great introduction.

Mike Caron
+85  A: 

Xcode includes OCUnit, an Objective-C unit testing framework, and support for running unit tests (OCUnit or otherwise) as part of your project's build process. Xcode's unit testing support is described in the Xcode Unit Testing Guide.

I've written a series of weblog posts about how to perform some common tasks with Xcode unit testing:

Finally, I've written a few posts on how to write tests for Cocoa user interfaces; the way Cocoa is structured makes it relatively straightforward, because you don't have to spin an event loop or anything like that in most cases.

This makes it possible to do test-driven development for not just your model-level code but also your controller-level and even view-level code.

Chris Hanson
Peter Hosey also gave a good presentation on Cocoa Unit Testing at the Lake Forest, CA Cocoaheads meeting in December. Here's a video of it:
Grant Limberg
+15  A: 

I started using the Google toolbox testing rig for iPhone, and its working out great for me.


Ryan Townshend
+2  A: 

Sen:te (the creator of the testing framework included with Xcode) explains how to use OCUnit with an iPhone project:

+16  A: 

Check out GHUnit by Gabriel Handford:

"The goals of GHUnit are:

Runs unit tests within XCode, allowing you to fully utilize the XCode Debugger. A simple GUI to help you visualize your tests. Show stack traces. Be installable as a framework (for Cocoa apps) with a simple (or not) target setup; or easy to package into your iPhone project."

Paul Robinson
+1 for GHUnit! I'm new to iPhone development and I can honestly say I had GHUnit up and running with no fuss (instructions are perfect) compared to the absolute nightmare of OCUnit and the mild migraine of GTM. GHUnit wins for usability and ease!
T Reddy
+5  A: 

Note that the Google Toolbox for Mac (GTM) project simply extends/augments Apple's SenTestingKit framework (which is, itself based on OCUnit). As they say on the project site:

GTM has several enhancement to the standard SenTestingKit allowing you to do UI unit testing, automated binding unit testing, log tracking, and unit testing on the iPhone, as well as tools for doing static and dynamic testing of your code.

Note the following comment about user-interface testing:

GTM has extensive support for user interface unit tests. It supports testing both the imaging and/or internal state of almost all of the standard Cocoa/UIKit UI objects, and makes it easy for you to extend this support to your own UI objects.

See their "Code Verification and Unit Testing" page for instructions on how to use it.

Clint Harris
+2  A: 

I would also recommend using coverage tools to see which part of the code are covered with unit tests and which are not. Basic line and branch code coverage can be generated with the GCOV tool. If you want to generate nice HTML coverage reports there are LCOV and ZCOV which do just that.

Nikita Zhuk
+1  A: 

I recommend gh-unit, it has a nice GUI for test results.

Joel Levin
+3  A: 

I came to the conclusion that GHUnit is the most advanced testing framework for Objective-C. I have done a roundup of testing frameworks on my blog. It is the most flexible in terms of deployment (iphone, simulator or mac os native) and assert capabilities. Because it is based on GTM, it inherits all of GTM's advantages over SenTestingKit but also adds a lot more. Another bonus is that it is being maintained very actively.

I have conducted effort to integrate OCMock into GHUnit, it works great!. You can get the code on github.

Johannes Rudolph
+1  A: 

Matt Gallagher of Cocoa with Love has a very good article on unit testing.

Mihir Mathuria
+1  A: 

I use SimpleUnitTest works with iPhone and iPad libs.

It comes with a unit test XCode template to easily add a unit test class. Wraps GTM.

You can literally drop it into an active project and start adding unit tests within 3 minutes (or less).

C. Bess
+3  A: 

The Unit Testing support bundled within xcode (for its simple setup) combined with ocrunner (for some autotest/Growl goodness) is currently my favorite Obj-C Unit Testing setup.

Steph Thirion