What is the best way to unit test Objective-C code?
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:
- Unit testing Cocoa frameworks
- Debugging Cocoa framework unit tests
- Unit testing Cocoa applications
- Debugging Cocoa application unit tests
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.
- Trust, but verify.
- Unit testing Cocoa user interfaces: Target-Action
- Unit testing Cocoa user interfaces: Cocoa Bindings
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.
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]
Comments
-
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 over 15 yearsPeter 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 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 about 13 yearsWhen 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 over 12 yearsEr.. is it supposed to fail as soon as you run it?
-
Robert about 12 yearsAre 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 almost 12 yearsXcode Unit Testing Guide link has moved to Xcode Unit Testing Guide
-
kevinthompson about 11 yearsNote 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 over 10 yearsKiwi also supports OS X, see github.com/allending/Kiwi/wiki/Up-and-Running-with-Kiwi-for-Mac
-
BrightIntelDusk over 10 yearsXcode 5 now has XCTest does it still include OCUnit? Will you update your answer with information for XCode 5.
-
Chris Hanson almost 8 yearsIf 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 about 6 yearsThis project has since been deprecated.