LoginSignup
19
19

More than 5 years have passed since last update.

XCTestについてのメモ

Last updated at Posted at 2015-08-06

テストターゲット内のリソースはMainBundleとは別扱い

pathの取得方法(取得先)が違うので注意が必要です。

let path = NSBundle(forClass: self.dynamicType).pathForResource("photo", ofType: "jpg")

追記:Swift3だと下記のようになります。

let path = Bundle(for: type(of: self)).path(forResource: "photo", ofType: "jpg")

非同期のテストについては、waitForExpectationsWithTimeoutを使う

2014年のWWDCに「Testing in Xcode 6」というセッションがあり、その中で Asynchronous testing として説明されています。
https://developer.apple.com/videos/wwdc/2014/#414

let expectation = expectationWithDescription("Save Image from UIImage")

PhotosHelper().saveImageToPhotosLibrary(image) { (success, error) in
    XCTAssertTrue(success)
    expectation.fulfill()
}

waitForExpectationsWithTimeout(5.0, handler: nil)

XCTAssertThrows等の例外を受けるアサートはなくなった

SwiftにはObjective-Cにはあった例外処理がなくなったので、実装されていないようです。

ググると、Objective-Cをラップした自作アサートの話が出てきました。
http://stackoverflow.com/questions/25529625/testing-assertion-in-swift

こちらも同様なサンプルコードです。
https://gist.github.com/akolov/8894408226dab48d3021


テストの実行は名前順?

手元の環境(Xcode6.4)では、XCTestCaseのサブクラスのクラス名順、さらにクラス内のテストケースも記述順ではなく名前順で実行されていることを確認しました。

  • Aテストクラス
    • テストケース名前順1
    • テストケース名前順2

   ↓

  • Bテストクラス
    • テストケース名前順1
    • テストケース名前順2
    • テストケース名前順3
    • テストケース名前順4

   ↓

  • Cテストクラス
    • テストケース名前順1
    • テストケース名前順2
    • テストケース名前順3

のような順で実行されました。

19
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
19