Xcodeでのテストが変わる! 同時起動やスクリーンショット機能が追加されました! #WWDC17

2017.06.09

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

こんにちは!
モバイルアプリサービス部の田中孝明です。

現地で参加した「What's New in Testing」のセッションのレポートになります。

本記事は Apple からベータ版として公開されているドキュメントを情報源としています。 そのため、正式版と異なる情報になる可能性があります。ご留意の上、お読みください。

What's New in Testing

Xcode 9 has new APIs for structuring your test logging and including your own attachments and screenshots, as well as new support for parallel device and simulator testing. Learn how to write UI tests that target multiple applications, and find out ways to improve the performance of your UI tests.


セッション動画に関しては以下で公開される予定です。
※現地時間6/8 18:00時点ではまだ公開されていません。

What's New in Testing

同時起動

テストターゲットの同時起動ができるようになりました。
こちらは是非実機動作させて試してみたいですね。

Asynchronous Tests and Expectations

ネットワークやアニメーションのテストなど、非同期で結果を受け取りたい時にXCTestExpectationを使う事で実現できるようになりました。

func testDownloadWebData() {

    // Create an expectation for a background download task.
    let expectation = XCTestExpectation(description: "Download apple.com home page")

    // Create a URL for a web page to be downloaded.
    let url = URL(string: "https://apple.com")!

    // Create a background task to download the web page.
    let dataTask = URLSession.shared.dataTask(with: url) { (data, _, _) in

        // Make sure we downloaded some data.
        XCTAssertNotNil(data, "No data was downloaded.")

        // Fulfill the expectation to indicate that the background task has finished successfully.
        expectation.fulfill()

    }

    // Start the download task.
    dataTask.resume()

    // Wait until the expectation is fulfilled, with a timeout of 10 seconds.
    wait(for: [expectation], timeout: 10.0)
}

Asynchronous Tests and Expectations

User Interface Tests

ユーザーインターフェースのテストを行うために、スクリーンショットを撮る機能が追加されました。

メイン画面を撮る場合です。

let screenshot = XCUIScreen.main.screenshot()

全てのスクリーンショットを撮る場合です。

let allScreenshots = XCUIScreen.screens.map { screen in
    return screen.screenshot()
}


User Interface Tests

最後に

多くの機能が追加されました。
非同期のテストやスクリーンショットを撮る機能がとうとう公式でサポートされました。
今まではサードパーティのテストフレームワークに頼っていたところがありましたが、公式のサポートは大きいですよね。
いずれ使い分けをするようなノウハウが公開されるのでしょうか?
この辺りも随時キャッチアップしてまいります。

ドキュメント

What's New in Testing
XCTest
Asynchronous Tests and Expectations
User Interface Tests