将测试文件添加到 Xcode 项目

创建项目时

你应该在项目创建对话框中选中包含单元测试

StackOverflow 文档

创建项目后

如果你在创建项目时错过了检查该项目,则可以随后添加测试文件。为此:

1-转到 Xcode 中的项目设置

2-转到目标

3-单击添加目标

4-在其他下,选择 Cocoa Touch Unit Test Testing Bundle

最后,你应该有一个名为 [Your app name]Tests.swift 的文件。在 Objective-C 中,你应该有两个名为 [Your app name]Tests.h[Your app name]Tests.m 的文件。

[Your app name]Tests.swift or .m 文件默认包含:

  • 一个 XCTest 模块导入
  • [Your app name]Tests 类延伸 XCTestCase
  • setUptearDowntestExampletestPerformanceExample 方法

迅速

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