Xcodeユニットテスト
2532 ワード
Xcodeユニットテスト
ユニットテストの追加
新規プロジェクト時に追加
既存のアイテムに追加
File->New->Target...
効果
基本プロセス
ユニットテストのクラス継承:XCTestCase
試験方法はtestで始まる override func setUp() {
//// , test case
super.setUp()
}
override func tearDown() {
// tearDown
super.tearDown()
}
func testExample() {
//
}
基本テストプロセス:
override func setUp() {
//// , test case
super.setUp()
}
override func tearDown() {
// tearDown
super.tearDown()
}
func testExample() {
//
}
//
XCTAssert(expression, format...)
// :
XCTAssertTrue(expression, format...)
XCTAssertFalse(expression, format...)
XCTAssertEqual(expression1, expression2, format...)
XCTAssertNotEqual(expression1, expression2, format...)
XCTAssertEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertNotEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertNil(expression, format...)
XCTAssertNotNil(expression, format...)
XCTFail(format...) // Fail
基本的なテスト
func testExample() {
let a : Int32! = 5
let b : Int32! = 10
let expected: Int32! = 16;
let c:Int32! = self.add(a: a, b: b)
XCTAssertEqual(c, expected, "add ");
}
非同期テスト
func testAsyn() {
let exp : XCTestExpectation = self.expectation(description: " ")
let queue: OperationQueue = OperationQueue.init()
queue.addOperation {
sleep(2)
XCTAssertLessThanOrEqual("1", "1")
exp.fulfill()
}
self .waitForExpectations(timeout: 3, handler: { (error: Error?) in
if error != nil {
print(" :\(error.debugDescription)")
}
})
}
パフォーマンステスト
func testPerformanceExample() {
// , block ,
self.measure {
var i: Int64 = 1
var r: Int64 = 1
while i < 100000000 {
r += i
i+=1
}
print("r=\(r)")
}
}
結果の表示&期待の調整
デモ
コードオーバーライド率
構成オプション:
Product->schemes->edit scheme
コードオーバーライドはファイル表示に基づいています:CodeCoverageを新規作成します.swiftはcheckNameメソッドを提供します.
テストコード:
func testCodeCoverage() {
CodeCoverage.checkName(name: "riverli")
}
結果を表示:
基本UIテスト
デモのダウンロード
こうりゅうぐん
モバイル開発交流群:264706196