Incorporate Unit-Testing in a Project
To unit-test your code,your project must include Unit-test bundles.These bundles contain the test methods that exercise the code in an app to ensure that it behaves correctly.
Setting Up Unit-Testing in a Project
After adding the unit-test target the project navigator shows a new group-with the same name as the target-that contains the source files that implements the temporary test case.The new target is listed in the project's target list.
This unit-test target can perform one type of unit testing logic or application not both.
Setting up Logic Unit Tests
To Set up a unit-test target to perform logic tests:
1.In the project editor select the unit-test target you want to set up,and display the build settings pane.
2.In the Build Settings pane's scope bar,click All.
3.In the search field enter bundle loader,and press Return.
4.If the Bundle Loader build setting appears in bold,select it and press Delete.
5.In the search field,enter test host and press Return.
6.If the Test host build setting appears in bold,select it and press Delete.
To confirm that the unit-test target is configured correctly to perform logic tests:
1.From the Scheme toolbar menu,choose the unit-tests scheme and a run destination:
IOS:For logic tests,the run destination can be only a simulator.
2.Choose Product > Test.
3.Choose View > Navigators > Issue,to display the issue navigator.
The issue navigator lists the failed temporary test case,indicating the unit-test target is working correctly.
Writing Test Case Methods
Structure of a Test case Method
-(void)test<test_case_name> {
.... // Set up,call test-case subject API.
ST...// Report pass/fail to testing framework.
.... // Tear down.
}
Example Of a setUp/tearDown method pair
-(void)setUp {
test_subject=[[MyClass alloc]init];
STAssertNotNil(test_subject,@"could not create test object");
}
-(void)tearDown {
[test_subject release];
}
Unit-Test Result Macro Reference
Unconditional Failure
STFail
Fails the test case.
STFail(failure_description,...)
Parameters
failure_description
Format string specifying error message.Can be nil.
Equality Tests
STAssertEqualObjects
Fails the test case when two objects are different.
Syntax:
STAssertEqualObjects(object1,object2,failure description,..)
Parameters
object_1
An object.
object_2
An object.
Failure_description
Format string specifying error message.Can be nil.
STAssertEquals
Fails the test case when two values are different.
Syntax:
STAssertEquals(value_1,value_2,failure_description,...)
Parameters
value_1
A scalar,structure or union.
value_2
A scalar,structure or union.
Failure description:
Format string specifying error message.Can be nil.
STAssertEqualsWithAccuracy
Fails the test case when the difference between two values is greater than the given value.
Syntax:
STAssertEqualsWithAccuracy(value1,value2,accuracy,failure description,..)
Parameters
value_1
An integer or a floating-point value.
value_2
An integer or a floating-point value.
accuracy
An integer or a floating-point value.
failure description
Format string specifying error message.Can be nil.
Nil Tests
STAssertNil
Fails the test case when a given expression is not nil.
Syntax:
STAssertNil(expression,failure description,....)
Parameters
expression
Expression to test.
failure_description
Format string specifying error message.Can be nil.
STAssertNotNil
Fails the test case when the given expression is nil.
Syntax:
STAssertNotNil(expression,failure_description,..)
Parameters
expression:
Expression to test.
failure_description:
Format string specifying error message can be nil.
Boolean Tests
STAssertTrue
Fails the test case when a given expression is false.
Syntax:
STAssertTrue(expression,failure description,...)
Parameters:
expression
Expression to test.
failure_description
Format string specifying error message.Can be nil.
STAssertFalse
Fails the test case when a given expression is true.
Syntax:
STAssertFalse(expression,failure_description,...)
Parameters
expression
Expression to test.
failure_description
Format string specifying error message.Can be nil.
Exception Tests
STAssertThrows
Fails the test case when an expression does not raise an exception.
Syntax:
STAssertThrows(expression,failure_description,...)
Parameters
expression
Expression to test.
failure_description
Format string specifying error message.Can be nil.
STAssertThrowsSpecific
Fails the test case when an expression does not raise an exception of a particular class.
Syntax:
STAssertThrowsSpecific(expression,exception_class,failure_description,...)
Parameters
expression
Expression to test.
exception_class
An exception class.
failure_description
Format string specifying error message.Can be nil.
STAssertThrowsSpecificNamed
Fails the test case when an expression doesn't raise an exception of a particular class with a given name.
Syntax:
STAssertThrowsSpecificNamed(expression,exception_class,exception_name,failure_description,..)
Parameters
expression
Expression to test.
exception_class
An exception class.
exception_name
A string with name of an exception.
failure_description
Format string specifying error message.Can be nil.
STAssertNoThrow
Fails the test case when an expression raises an exception.
Syntax:
STAssertNoThrow(expression,failure_description,...)
Parameters
expression
Expression to test.
failure_description
Format string specifying error message.Can be nil.
STAssertNoThrowSpecific
Fails the test case when an expression raises an exception of a particular class.
Syntax:
STAssertNoThrowSpecific(expression,exception_class,failure_description,...)
Parameters
expression
Expression to test.
exception_class
An exception class.
failure_description
Format string specifying error message.Can be nil.
STAssertNoThrowSpecificNamed
Fails the test case when an expression raise an exception of a particular class with a given name.
Syntax:
STAssertNoThrowSpecificNamed(expression,exception_class,exception_name,failure_description,...)
Parameters
expression
Expression to test.
exception_class
An exception class.
exception_name
A string with the name of an exception.
failure_description
Format string specifying error message.Can be nil.
STAssertTrueNoThrow
Fails the test case when an expression is false or raises an exception.
Syntax:
STAssertTrueNoThrow(expression,failure_description,...)
Parameters
expression
Expression to test.
failure_description
Format string specifying error message.Can be nil.
STAssertFalseNoThrow
Fails the test case when an expression is true or raises an exception.
Syntax
STAssertFalseNoThrow(expression,failure_description,...)
Parameters
expression
Expression to test.
failure_description
Format string specifying error message.Can be nil.