What is the best way to unit test Objective-C code?

74,478

Solution 1

Xcode includes XCTest, which is similar to OCUnit, an Objective-C unit testing framework, and has full support for running XCTest-based unit tests as part of your project's build process. Xcode's unit testing support is described in the Xcode Overview: Using Unit Tests.

Back in the Xcode 2 days, I wrote a series of weblog posts about how to perform some common tasks with Xcode unit testing:

Despite using OCUnit rather than XCTest, the concepts are largely the same.

Finally, I also wrote 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.

Solution 2

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."

Solution 3

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

google-toolbox-for-mac

Solution 4

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

Solution 5

I realize this is an old question, but if you prefer BDD-style testing (rspec, Jasmine, etc.) over xUnit-style testing (Test::Unit, JSUnit, JUnit, etc.), then you may consider checking out Cedar. Cedar brings BDD-style testing to Objective-C, now that the language supports closures.

We're happily using Cedar for our iOS projects at Pivotal Labs, and we're actively working on improving it. Any feedback or suggestions are welcome at [email protected]

Share:
74,478
Santhosh
Author by

Santhosh

Smalltalk programmer

Updated on March 06, 2020

Comments

  • Santhosh
    Santhosh over 4 years

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

  • Grant Limberg
    Grant Limberg over 15 years
    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: vimeo.com/2725498
  • Tim Reddy
    Tim Reddy almost 14 years
    +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!
  • drekka
    drekka about 13 years
    When I started iOS stuff about a year ago I found that OCUnit had (for me) a serious drawback - I could not run it on the simulator or device. It may have changed since then - I haven't checked, but I've been using GHUnit simply because of that reason.
  • quantumpotato
    quantumpotato over 12 years
    Er.. is it supposed to fail as soon as you run it?
  • Robert
    Robert about 12 years
    Are there any official Apple Developer videos on this subject? I went through WWDC 2011 and 2010 but couldn't find any... strange since testing is important yes?
  • Steven Green
    Steven Green almost 12 years
    Xcode Unit Testing Guide link has moved to Xcode Unit Testing Guide
  • kevinthompson
    kevinthompson about 11 years
    Note that this answer and the accompanying comments were written some time ago. Xcode has much better support for OCUnit-based tests now, making it easy to set up tests and run them in the debugger.
  • dbainbridge
    dbainbridge over 10 years
  • BrightIntelDusk
    BrightIntelDusk over 10 years
    Xcode 5 now has XCTest does it still include OCUnit? Will you update your answer with information for XCode 5.
  • Chris Hanson
    Chris Hanson almost 8 years
    If you have enough reputation, you should be able to update it. Xcode 5 continued to include OCUnit, we removed it in either Xcode 6 or Xcode 7. We have a converter, however, and the concepts behind the frameworks remain the same, and at least some of my blog posts (which were written in the Xcode 2 & 3 days) remain.
  • BigHeadCreations
    BigHeadCreations about 6 years
    This project has since been deprecated.