01Adding Test Files to Xcode Project
suggest changeWhen creating the project
You should check “Include Unit Tests” in the project creation dialog.
![](/essential/ios/img/621392d668555bf21c28c4b0c95a92bcf9236b1d.png)
After creating the project
If you missed checking that item while creating your project, you could always add test files later. To do so:
1- Go to your project settings in Xcode
2- Go to “Targets”
3- Click “Add Target”
4- Under “Other”, select “Cocoa Touch Unit Test Testing Bundle”
At the end, you should have a file named [Your app name]Tests.swift
. In Objective-C, you should have two files named [Your app name]Tests.h
and [Your app name]Tests.m
instead.
[Your app name]Tests.swift or .m
file will include by default :
- A
XCTest
module import - A
[Your app name]Tests
class which extendsXCTestCase
setUp
,tearDown
,testExample
,testPerformanceExample
methods
Swift
import XCTest
class MyProjectTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}
Objective-C
#import <XCTest/XCTest.h>
@interface MyProjectTests : XCTestCase
@end
@implementation MyProjectTests
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}
@end
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents