myCodeStudio
2Aug/090

Unit Tests for the iPhone

In XCode 3.0 (not sure when they added it), there are options to add Unit Tests to your iPhone application but I was not able to get this to work properly using the iPhone Unit Test target. However I did find a reference to add Cocoa Unit Tests to your iPhone project. If you follow the link, you will find all the information you need. It's quick to add to your project and will allow your tests to run every time you build.

Google Framework

After some further work, the above setup allows easy running of unit tests. However, debugging those Unit Tests is another story. I found another page that walks
through using the Google Toolkit for Mac to do unit testing. This allows you to run the unit tests as an executable and at least output console output (still can't seem to debug it). Note that with this framework, you need to set the active executable to your Testing executable in order to run. Any NSLog calls will be output to the console.

Unit Test Assertions

The table below is a quick reference to the different types of assertions that can be done in the UnitTest.

OCUnit Macro Purpose
STAssertNil(a1, description, ...) Generates an error if a1 is not nil.
STAssertNotNil(a1, description, ...) Generates an error if a1 is nil.
STAssertTrue(expression, description, ...) Generates an error if expression does not evaluate to true
STAssertFalse(expression, description, ...) Generates an error if expression does not evaluate to false
STAssertEqualObjects(a1, a2, description, ...) Generates an error if a1 is not equal to a2. Both must be Objective-C objects.
STAssertEquals(a1, a2, description, ...) Generates an error if a1 is not equal to a2. Both must be C scalar values.
STAssertEqualsWithAccuracy(left, right, accuracy, description, ...) Generates an error if a1 and a2 are not within a certain amount of each other. Primarily for use with floats and doubles to take into account small rounding errors due to the way they store values.
STAssertThrows(expression, description, ...) Generates an error if expression does not throw an exception.
STAssertThrowsSpecific(expression, specificException, description, ...) Generates an error if expression does not throw an exception of a specified class.
STAssertThrowsSpecificNamed(expr, specificException, aName, description, ...) Generates an error if expression doesn’t throw an exception with a specific name.
STAssertNoThrow(expression, description, ...) Generates an error if expression throws an exception.
STAssertNoThrowSpecific(expression, specificException, description, ...) Generates an error if expression throws an exception of a specified class.
STAssertNoThrowSpecificNamed(expr, specificException, aName, description, ...) Generates an error if expression throws an exception with a specific name.
STFail(description, ...) Generates an error.
STAssertTrueNoThrow(expression, description, ...) Generates an error if expression is false or if it throws an exception.
STAssertFalseNoThrow(expron, description, ...) Generates an error if expression is true or if it throws an exception

Table 1: Macros Available in OCUnit from Apple

About Administrator

No description. Please complete your profile.
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


*

No trackbacks yet.