How to implement XCTestCase in Xcode Project?

1. First, you have to import XCTest.framwork in project.

2. Now, go into “Project Tests” section on left section of Xcode. (EX. “Sample Tests”)

3. Now, right click on that tests and create new file with “Test case Class”.



4. The name of that particular file is same as view controller with Appending “test” at last. (EX. If my view controller name is “ViewController.m” than test case file is like “ViewControllerTest.m”).

5. Now, import ViewController.h in test file.

#import "ViewController.m"

6. Declare the variable of ViewController in test file.

@interface ViewControllerTest : XCTestCase
@property ViewController *testBTHandler;

7. Implement methods in this test file. Method name should always start with “test”.


- (void)testInitObject
XCTAssertNotNil([self testBTHandler], @"Init Failed.");

8. Now, go into “Product-> Test”.

9. Project is going to be tested.

10. Following are the functions of XCTestCase:

▪ XCTFail (format…)
▪ XCTAssertNil (a1, format…)
▪ XCTAssertNotNil (a1, format…)
▪ XCTAssert (a1, format…)
▪ XCTAssertTrue (a1, format…)
▪ XCTAssertFalse (a1, format…)
▪ XCTAssertEqualObjects (a1, a2, format…)
▪ XCTAssertEquals (a1, a2, format…)
▪ XCTAssertEqualsWithAccuracy (a1, a2, accuracy, format…)
▪ XCTAssertThrows (expression, format…)
▪ XCTAssertThrowsSpecific (expression, specificException, format…)
▪ XCTAssertThrowsSpecificNamed (expression, specificException, exceptionName, format…)
▪ XCTAssertNoThrow (expression, format…)
▪ XCTAssertNoThrowSpecific (expression, specificException, format…)
▪ XCTAssertNoThrowSpecificNamed (expression, specificExcepton, exceptionName, format…)