在開專案的時候要勾 Include Tests,這樣就會附上 Tests, UITests Targets。
如果是已經存在的專案,一開始並沒有勾 include tests,你還是可以在之後加上 test targets
在增加 Target 後,專案會出現預設的 test code
import XCTest
final class YouNeedTestingTests: XCTestCase {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
}
func testPerformanceExample() throws {
// This is an example of a performance test case.
measure {
// Put the code you want to measure the time of here.
}
}
}
試著選擇 Test Navigator 並按下 Test,確定 Unit Testing 能運作。這樣,就完成了在專案中加入 Unit Testing 步驟。