[Biococoa-dev] Unit testing

Charles PARNOT charles.parnot at stanford.edu
Sat Feb 19 02:54:35 EST 2005

>Is there a good resource online to read up on the
>principles and practices of unit testing?  One that's appropriate for
>someone with about 5 minutes of free time on my average day?

I liked that page too:
(a Fixture = how you prepare your TestCase instance before running a 
test method)

I am myself very new to it, and found the concept easy to grasp, and 
the implementation quite straightforward. Experience is probably 
needed to have a good sense of what deserves a test and what does not 
for a particular class in a particular project. Doing too much is a 
waste. Doing too little is a waste too if you end up not using the 
tests or not getting much from it.

All the different unit test frameworks available are very similar. 
There are 4 important components:

* A bundle provided by you. This bundle is a collection of classes, 
most if not all are 'TestCase' (see below). This 'test bundle' that 
you create is usually separate from the code you want to test. In our 
case, we would thus have two bundles: BioCocoa.framework and 
BioCocoa-test.bundle. The second bundle is the one you provide to the 
'Tester' tool (see next entry) and that is being scanned for tests 
that need to be run.

* A tool provided by the testing kit (an executable ready for use) or 
'Tester' tool. You run that tool by providing it a bundle: it loads 
that bundle in memory, then scans all the classes and all the 
methods, looking for the code that needs to be run for the tests (see 
below the criteria that are used). So it is smart enough so you don't 
have to list all the tests anywhere, they are 'automatically' 
detected. The toool then runs the tests that were detected and logs 
the result as they are run, with some details about the failed tests. 
It counts how many tests ran, how many failed, and how long they took.

* TestCase class.
You are supposed to subclass this class to write tests. A test case 
is... well, a test case. You load some data (=initialize the test 
case), you run some method on it, you check the result. You load 
data, you run another method on it, you check the result... For each 
test, you reload the data. This is done automatically by the 'Tester' 
tool mentioned above. If the tool determines that your class is a 
subclass of TestCase, it will look for all the methods that start 
with 'test' and for each of these methods, it will create an 
instance, run the first test method, dealloc the instance. Then 
again, create an instance, run the second test method, dealloc the 
instance.... All this for all the subclasses of TestCase found in the 
This means that you have to write some methods with names starting 
with 'test' and that will run some code. And the tool takes care of 
running the test for you.

* Assertion macros.
Inside your 'test' methods, you will test things, right? Well, to 
tell the 'Tester' program how things are going, you call macros for 
your tests, e.g. Assert_Equality(object1,object2). These macros 
notifies the 'Tester' tool of the failures, and useful log messages 
are being displayed.

Now, some code!
Here is my first test class and test bundle ever:
(you have to install OCUnit first and maybe adjust the paths)


Help science go fast forward:

Charles Parnot
charles.parnot at stanford.edu

Room  B157 in Beckman Center
279, Campus Drive
Stanford University
Stanford, CA 94305 (USA)

Tel +1 650 725 7754
Fax +1 650 725 8021

More information about the Biococoa-dev mailing list