Sponsor Link
目次
環境&対象
- macOS Catalina 10.15.7
- Xcode 12.2RC
プロジェクトにテストを設定する方法
Xcode のプロジェクトにテストを設定する方法を説明します。
「新規プロジェクト作成時に設定する方法」と「既存プロジェクトに追加で設定する方法」の2つがあります。
新規プロジェクトの時に設定する方法
Xcode で新規プロジェクトを選択すると、以下のようなステップでプロジェクトを作成します。
- “Choose a template for your new project” で、プラットフォーム / プロジェクトのタイプを選択
- “Choose options for your new project” で、Product Name や Interface 等を選択
- プロジェクト保存場所の選択と Git リポジトリを作るかどうかの選択
手順2の箇所で、”include Tests” を選択することで、テスト用のターゲットとテスト用のコードが追加されます。
この操作では、UITest と UnitTest の2つのテストが追加されます。
既存プロジェクトに追加で設定する方法(新規テストターゲットを追加する)
以下の手順で、追加していきます。
- プロジェクトナビゲータ(⌘1)で、プロジェクトを選択
- “Choose a template for your new target” で、”UI Testing Bundle” もしくは、”Unit Testing Bundle” を選択
- “Choose options for your new target” で、新しいテストターゲットの詳細を設定
以下は、手順2で、UI Testing Bundle を選んでいる画面
テンプレートで生成されているコードの意味
UnitTest
プロジェクト名 Empty というプロジェクトでは、EmptyTests というターゲット名で UnitTest のコードが以下のように生成されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// // EmptyTests.swift // // Created by : Tomoaki Yagishita on 2020/11/11 // © 2020 SmallDeskSoftware // // (1) import XCTest class EmptyTests: XCTestCase { // (2) override func setUpWithError() throws { // Put setup code here. This method is called before the invocation of each test method in the class. } // (3) override func tearDownWithError() throws { // Put teardown code here. This method is called after the invocation of each test method in the class. } // (4) 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. } // (5) func testPerformanceExample() throws { // This is an example of a performance test case. measure { // Put the code you want to measure the time of here. } } } |
- 対象のアプリ/モジュールの内部クラス等をテストするためには、@testable import <modlue name> を追加する必要があります
- テスト用のセットアップコードを記載します。各テスト関数実行前に実行されます。
- テスト用の終了コードを記載します。書くテスト関数実行後に実行されます。
- テスト関数の例です。”test” という名称で開始することが必要です。ここでのコードは、空の関数になっています
- パフォーマンスをテストするためのコード例になっています。measure で指定された closure が測定対象となります。
UITest
プロジェクト名 Empty というプロジェクトでは、EmptyUITests というターゲット名で UITest のコードが以下のように生成されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
// // EmptyUITests.swift // // Created by : Tomoaki Yagishita on 2020/11/11 // © 2020 SmallDeskSoftware // // (1) import XCTest class EmptyUITests: XCTestCase { // (2) override func setUpWithError() throws { // Put setup code here. This method is called before the invocation of each test method in the class. // In UI tests it is usually best to stop immediately when a failure occurs. continueAfterFailure = false // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. } // (3) override func tearDownWithError() throws { // Put teardown code here. This method is called after the invocation of each test method in the class. } // (4) func testExample() throws { // UI tests must launch the application that they test. let app = XCUIApplication() app.launch() // Use recording to get started writing UI tests. // Use XCTAssert and related functions to verify your tests produce the correct results. } // (5) func testLaunchPerformance() throws { if #available(macOS 10.15, iOS 13.0, tvOS 13.0, *) { // This measures how long it takes to launch your application. measure(metrics: [XCTApplicationLaunchMetric()]) { XCUIApplication().launch() } } } } |
- UITest は、アプリ/モジュールを外部からテストするため、@testable import <modlue name> を追加する必要は、ありません。
- テスト用のセットアップコードを記載します。各テスト関数実行前に実行されます。
- テスト用の終了コードを記載します。書くテスト関数実行後に実行されます。
- テスト関数の例です。”test” という名称で開始することが必要です。ここでのコードは、空の関数になっています
- パフォーマンスをテストするためのコード例になっています。measure で指定された closure が測定対象となります。
テストを実行する方法/確認する方法
テストを実行する方法を説明します。
プロジェクトに含まれる全てのテストを実行する方法
Xcode メニューの [Product]-[Test] (⌘U) を選択すると、プロジェクトに含まれる全てのテストが実行されます。
テストクラスに含まれる全てのテストを実行する方法
テストクラスの左側にマウスをホバーすると表示される実行可能シンボルをクリックすることで、該当クラスに含まれるテストを実行することができます。
1つのテスト関数を実行する方法
テストメソッドの左側にマウスをホバーすると表示される実行可能シンボルをクリックすることで、該当テスト関数を実行することができます。
テストの結果を確認する方法
Tests ナビゲータ(⌘6) でテスト結果を確認することができます。
新しいテストを追加する方法
最初に作成されたファイル以外でも、XCTestCase を継承したクラスで、test で始まるメソッドであればテスト対象となります。
既存ファイルに、新規テスト関数追加
test で始まる新しいメソッドを定義すると、自動的にテスト対象となります。
UnitTest / UITest いずれの場合も、既存の setUpWithError/tearDownWithError は、テスト前後に実行されますので、意識する必要があります。
以下のようなメソッド名をつけると、なんのテストをしているかわかりやすくなるので、おすすめです。
テスト内部は、Arrange: “事前準備” -> Act: “テスト対象操作実行” -> Assert: “テスト結果確認” という順番を守って記述するのがおすすめです。
1 2 3 4 5 |
func test_<#methodName#>_<#withCertainState#>_<#shouldDoSomething#>() { <#Arrange#><#,Act#><#,Assert#> } |
新規ファイル追加
プロジェクトへ新規ファイルを追加します。特に、専用メニュー等はありません。
新規ファイルを作成する時に、”UI Test Cacse Class” と “Unit Test Case Class” というテンプレートが選択できますが、これらのテンプレートは、Objective-C 用のファイルを作成しますので、Swift で記述するのであれば、”Swift File” を選択して新規ファイルを追加します。
以下は、自分向けに作成した新規 UnitTest 向けのテンプレート と UITest 向けのテンプレートです。
Xcode は、自動で作ってくれませんので、自分でこのようなテンプレートを作っておくと便利です。
なお、テンプレートは、パフォーマンステスト向けのコードを含んでいません。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// // #MyOwn#Tests.swift // // Created by : Tomoaki Yagishita on 2020/11/11 // © 2020 SmallDeskSoftware // import XCTest @testable import #MyModule# class #MyOwn#Tests: 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. } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// // #MyOwn#UITests.swift // // Created by : Tomoaki Yagishita on 2020/11/11 // © 2020 SmallDeskSoftware // import XCTest class #MyOwn#UITests: XCTestCase { override func setUpWithError() throws { // Put setup code here. This method is called before the invocation of each test method in the class. // In UI tests it is usually best to stop immediately when a failure occurs. continueAfterFailure = false // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. } 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 { // UI tests must launch the application that they test. let app = XCUIApplication() app.launch() // Use recording to get started writing UI tests. // Use XCTAssert and related functions to verify your tests produce the correct results. } } |
まとめ: Xcode を使ったテストの始め方
- 新規プロジェクトでは、”includes Test” にチェックを入れる
- 既存プロジェクトには、新規ターゲット追加でテストを追加する
- UITest と UnitTest がある
説明は以上です。Happy Coding !
Sponsor Link