將測試檔案新增到 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