iOS UnitTest(XCTest)

XCTest is XCode5, iOS7 feature for Test.
When you create new project(included test), you can see Test Case.

Test

When creating new project, test is automatically generated.

XCode XCTest

Look at above. CalendarTest is test.
CalendarTest.m is test class.

Run Test

By default, there is failed test case in test project.
Let’s start this.
To run test, you long press “Run” and switch to “Test” button.
Above picture shows Test button.
After changing “Test” button, press test button(not long press).
At first time, you can see link error I thought.(in my case).

Link error

To try test, but an error is coming.
unrecognized selector sent to class
It seems that something forget to link.
This is not your fault.
There is nice solution on biasedbit.
We need to add 2 linker flags in test project. -ObjC, -all_load

Result

Start test, emulator opens and run test.
After finishing, you can see result.
Press balloon button which are arranged same line of project and search…
The details are there.
You can also see project perspective view.

Success

XCTest Success

Failed

You can see place the test failed.
XCTest Failed

Write test

You can implement class which extends XCTestCase
There are some prepared methods for test

Method Description
setUp Start up, common initialization
tearDown Finish up, common operations when finishing test(clean)

This is like JUnit.

All test case method’s name should start with “test”
This is example

- (void)testBeginEnd {
  XCTFail("Unfortunately, failed");
}

If you add other name methods, they won’t be executed.

Assert Methods

Same as other platforms, they prepared Assert methods to use in Test.

Method Description
XCTFail(format…) Always fail
XCTAssertTrue(expression, format…) The expression is expected true
XCTAssertFalse(expression, format…) The expression is expected false
XCTAssertNil(a1, format…) The expression is expected nil
XCTAssertNotNil(a1, format…) The expression is expected not nil
XCTAssertEqual(a1, a2, format…) a1 and a2 is expected to be equal
XCTAssertNotEqual(a1, a2, format…) a1 and a2 is expected not to be equal
XCTAssert(expression, format…) The expression is expected true
XCTAssertEqualObjects(a1, a2, format…) a1 and a2 is expected to be same object
XCTAssertNotEqualObjects(a1, a2, format…) a1 and a2 is expected not to be same object
XCTAssertEqualWithAccuracy(a1, a2, accuracy, format…) The differences between a1 and a2 is expected to be within accuracy.
XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format…) The differences between a1 and a2 is expected not to be within accuracy.
XCTAssertThrows(expression, format…) expression is expected to throw exception
XCTAssertNoThrow(expression, format…) expression is expected not to throw exception
XCTAssertThrowsSpecific(expression, specificException, format…) expression is expected to throw “specificException”
XCTAssertNoThrowSpecific (expression, specificException, format…) expression is expected not to throw “specificException”
XCTAssertThrowsSpecificNamed (expression, specificException, exceptionName, format…) expression is expected to throw “exceptionName” exception
XCTAssertNoThrowSpecificNamed (expression, specificExcepton, exceptionName, format…) expression is expected not to throw “exceptionName” exception

Enjoy Testing!