Edit AllPages

General/UnitTesting is a way of testing your own software while developing. Using a fairly simple Framework each class gets exercised as thoroughly as necessary. The testing code is separate from the class code but can be run at any time. It is a Tool for General/TestFirstProgramming, a methodology that is part of General/ExtremeProgramming.

The idea of General/UnitTesting is that ALL the tests should be kept operational all the time. This means if you change something, do not work on something else while there is a test that fails. General/UnitTesting is not debugging, in debugging you try to find where your code is not working correctly, with unit testing you are asserting which parts of your code are working correctly. If you find a bug, first write a test and then fix the bug, this way if you reintroduce that bug it will be flagged immediately.

One problem of General/UnitTesting is how to test UI elements. There are some parts that cannot be easily automated, although with the help of delegates one might be able to do some automatic tests even on the UI.

General/TestFirstProgramming is a concept that makes you write the test before you write the actual software. The idea is that if an interface cannot be used in the test it should not be used at all. This idea from General/ExtremeProgramming forces you to use your interface before you actually code it. Also it gives you the tools to refactor.

There are several frameworks for General/ObjectiveCee that enable (encourages) General/UnitTesting

Other frameworks/source that are useful for people doing unittesting:

Starting with Mac OS X 10.4 and General/XCode 2.1 Apple included General/OCUnit (= General/SenTestingKit) with their developer tools.

Documentation includes html files in General/SenTestingKit.framework

With this news the developer of General/UnitKit seems to have decided to discontinue development and to submit patches to General/OCUnit instead.

See for a September 2004 discussion of the merits and differences between the popular testing frameworks. In particular, this illuminates the differences between General/OCUnit and General/MPWTest.

Empirically, the most popular frameworks seem to be General/OCUnit and General/ObjCUnit.


-Roman Bertolami

Here is an example of a test case using General/ObjCUnit. We are going to test some of the functionality of General/NSMutableArray:

@interface General/NSMutableArrayTest : General/TestCase { General/NSMutableArray *empty; General/NSMutableArray *full;

} @end

@implementation General/NSMutableArrayTest

// setUp gets run every time before a testcase

// tearDown gets run every time after a testcase

// start testcases with the word “test” they will be called // automatically

// and so on …


The assert functions check if the Object you give equals the second parameter, and print a warning if it doesn’t.

Hey, ummm…what’s with the weird assertion messaging? There’s no target.

Test cases can be grouped in suites that are run one after the other. Each test case can contain some setup code that gets run for each case.

Usually a suite is used to run all the tests. It looks like the following.

@interface General/AllTests : General/TestSuite

@implementation General/AllTests

The suite is run using

General/TestRunnerMain(General/[AllTests class]);

This runs all the tests in the suite and tallies up the successes and failures of all tests. You can either integrate that in your software so the tests are run every time it is started, or compile it into a separate Tool.

I hope this helps – General/HaRald

I would love to see a complete example of a Cocoa app along with its unit tests. Any pointers?

Here’s one pointer: Source code for General/OCUnit includes a complete set of unit tests for the package. As an added bonus, their Objective-C code is exceptionally nice and clean. It’s worth a read just to see how pretty good Objective-C code can be.

New Question: Is their code good because they develop with unit tests? Or do they develop clean code with unit tests because they’re channelling some wicked Smalltalk voodoo?

I don’t know. But whatever it is, you want some too.

General/GoogleToolboxForMac also is fully unit tested, and shows how to do UI unit tests, binding unit tests, Log Tracking and even how do to unit tests on the iPhone (simulator and device).

(Also related to unit testing) Added an General/XcodeGcovTutorial. – General/MikeAmy

I knew there was a response to this somewhere:

and some responses to the above: