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

17

333

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

Libbie answered 28/8, 2008 at 19:41 Comment(1)
To the best of my knowledge the some answers here are outdated. Apple now has XCTest, which helps you address this issue. I think most answers need to be updated.Splashy
D
317

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.

Definitive answered 28/8, 2008 at 19:52 Comment(6)
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/2725498Pitchdark
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.Vicechairman
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?Stalingrad
Xcode Unit Testing Guide link has moved to Xcode Unit Testing GuideContrabandist
Xcode 5 now has XCTest does it still include OCUnit? Will you update your answer with information for XCode 5.Rachaba
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.Definitive
C
50

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

Catheryncatheter answered 25/2, 2009 at 13:53 Comment(3)
+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!Wordage
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.Vinegarish
This project has since been deprecated.Social
G
18

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

google-toolbox-for-mac

Gaskin answered 19/11, 2008 at 16:59 Comment(0)
M
13

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

Meter answered 28/8, 2008 at 19:46 Comment(0)
R
12

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.

Riccio answered 2/3, 2009 at 19:2 Comment(0)
S
12

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.

Skipbomb answered 24/10, 2009 at 17:48 Comment(0)
E
12

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]

E answered 23/11, 2010 at 7:25 Comment(0)
L
10

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.

Lexicography answered 14/3, 2009 at 21:47 Comment(0)
E
7

Sen:te (the creator of the testing framework included with Xcode) explains how to use OCUnit with an iPhone project: simple-iphone-ipad-unit-test.

Erechtheum answered 18/1, 2009 at 15:32 Comment(0)
D
7

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

http://github.com/gabriel/gh-unit/tree/master

Dairyman answered 14/3, 2009 at 21:57 Comment(0)
D
7

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.

Depreciable answered 10/8, 2010 at 5:58 Comment(0)
C
7

here is a whole lot of them

List_of_unit_testing_frameworks in Objective-C

Cerement answered 26/4, 2011 at 3:43 Comment(0)
G
6

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

Gibe answered 23/12, 2009 at 18:39 Comment(0)
F
5

I would suggest looking into Kiwi, an open source BDD testing framework for iOS: Kiwi

Check out the project's WIKI to start or get Daniel Steinberg's book "Test Driving iOS Development with Kiwi" test-driving-ios-development

Flattish answered 23/2, 2013 at 23:28 Comment(1)
Kiwi also supports OS X, see github.com/allending/Kiwi/wiki/Up-and-Running-with-Kiwi-for-MacRosaceous
K
4

I use SimpleUnitTest works with iPhone and iPad libs.

http://cbess.blogspot.com/2010/05/simple-iphone-ipad-unit-test.html

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

Kidnap answered 18/5, 2010 at 23:52 Comment(1)
Er.. is it supposed to fail as soon as you run it?Biathlon
R
3

Specta is a modern TDD(Test Driven Development)/BDD(Behavior Driven Development) framework which runs on top of XCTest. It supports unit testing for iOS and Mac OS X projects.

Rosaceous answered 15/3, 2014 at 16:5 Comment(0)
A
0

I hope u can use 'SenTestKit', from which u can test each and every method.

Amputate answered 14/6, 2013 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.