Dependencies:
Get brushed up before starting:
All test classes have two methods in common setUp & tearDown.
setUp runs before every testcase & tearDown after every testcase.
Test cases runs alphabetically.
In Test Driven Development, it is good to create dummy test data first.
Test case methods starts with "test" keyword.
Test methods accept no parameters & return no value.
Appendix:
There are several other methods for comparing the expected result & actual result out of an operation. Some of those methods are listed below:
- (void)testReverseString{
NSString *originalString = @"hi_my_name_is_siddharth";
NSString *reversedString = [self.someObject reverseString:originalString];
NSString *expectedReversedString = @"htrahddis_si_eman_ym_ih";
XCTAssertEqualObjects(expectedReversedString, reversedString, @"The reversed string did not match the expected reverse");
}
Feed the dummy data to the method under test if required & then compare the expected & actual results.
- (void)testDoSomethingThatTakesSomeTime{
XCTestExpectation *completionExpectation = [self expectationWithDescription:@"Long method"];
[self.someObject doSomethingThatTakesSomeTimesWithCompletionBlock:^(NSString *result) {
XCTAssertEqualObjects(@"result", result, @"Result was not correct!");
[completionExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
1. For Synchronous methods :
- (void)testPerformanceReverseString {
NSString *originalString = @"hi_my_name_is_siddharth";
[self measureBlock:^{
[self.someObject reverseString:originalString];
}];
}
2. For Asynchronous methods :
- (void)testPerformanceOfAsynchronousBlock {
[self measureMetrics:@[XCTPerformanceMetric_WallClockTime] automaticallyStartMeasuring:YES forBlock:^{
XCTestExpectation *expectation = [self expectationWithDescription:@"performanceTestWithResponse"];
[self.someObject doSomethingThatTakesSomeTimesWithCompletionBlock:^(NSString *result) {
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
}];
}];
}
Run all tests by choosing Product > Test. Click the Test Navigator icon to view the status and results of the tests. You can add a test target to a project (or add a class to a test) by clicking the Add (plus) button in the bottom-left corner of the test navigator. To view the source code for a particular test, select it from the test list. The file opens in the source code editor.