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

Objective CCocoaUnit TestingXcode

Objective C Problem Overview


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

Objective C Solutions


Solution 1 - Objective C

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 - Objective C

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 - Objective C

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

google-toolbox-for-mac

Solution 4 - Objective C

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

Solution 5 - Objective C

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.

Solution 6 - Objective C

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.

Solution 7 - Objective C

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]

Solution 8 - Objective C

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.

Solution 9 - Objective C

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.

Solution 10 - Objective C

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

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

Solution 11 - Objective C

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.

Solution 12 - Objective C

Solution 13 - Objective C

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

Solution 14 - Objective C

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

Solution 15 - Objective C

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

Solution 16 - Objective C

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.

Solution 17 - Objective C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKimView Question on Stackoverflow
Solution 1 - Objective CChris HansonView Answer on Stackoverflow
Solution 2 - Objective CelasticratView Answer on Stackoverflow
Solution 3 - Objective CRyan TownshendView Answer on Stackoverflow
Solution 4 - Objective CMike CaronView Answer on Stackoverflow
Solution 5 - Objective CClint HarrisView Answer on Stackoverflow
Solution 6 - Objective CJohannes RudolphView Answer on Stackoverflow
Solution 7 - Objective CAdam MilliganView Answer on Stackoverflow
Solution 8 - Objective CNikita ZhukView Answer on Stackoverflow
Solution 9 - Objective CnstView Answer on Stackoverflow
Solution 10 - Objective CJoel LevinView Answer on Stackoverflow
Solution 11 - Objective CSteph ThirionView Answer on Stackoverflow
Solution 12 - Objective Cuser709682View Answer on Stackoverflow
Solution 13 - Objective CMihir MathuriaView Answer on Stackoverflow
Solution 14 - Objective CKrzysztof GrzywaczView Answer on Stackoverflow
Solution 15 - Objective CC. BessView Answer on Stackoverflow
Solution 16 - Objective CdbainbridgeView Answer on Stackoverflow
Solution 17 - Objective CKarthikView Answer on Stackoverflow