[iOS] 明確で簡潔なテストを書くことができる iOS UI自動化テスト・フレームワークEarlGreyを試してみました。

2016.03.12

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

1 はじめに

先月2月16日、「Google テストブログ」で公開された、EarlGreyは、明確で簡潔なテストを書くことができるiOS UI自動化テスト・フレームワークです。

Google Testing Blog
EarlGrey - iOS 向けの UI 機能テスト フレームワーク

同ブログで紹介されていた、EarlGreyの主な機能は、次のとおりです。

(1) 強力な組み込みの同期

テストはUIと対話する前のアニメーションや、ネットワーク接続などのイベントを自動的に待機します。 この際、特にスリープなどを書く必要がないため、テストコードはシンプルになります。

(2) 可視性のチェック

すべての操作はユーザーの可視範囲で発生します。 例えば、画像の背後にあるボタンをタップしようとすると、テストは失敗します。

(3) 柔軟な設計

要素の選択、相互作用、アサーション、同期について、コンポーネントが拡張できるように設計されています。

EarlGreyは、既にApacheライセンスでオープンソースとして、GitHubに公開されています。
https://github.com/google/EarlGrey

2 デモの実行

とりあえず、GitHubのリポジトリには、デモが含まれていましたので、それを実行してみました。

README.mdには、依存関係をダウンロードしてEarlGreyをコンパイルするところまで説明されていましたが、実は、EarlGreyは、わざわざ環境構築しなくても、Cocoapodsで簡単に取り込めるようになっているため、今回は、この方法でやってみました。

(1) ダウンロード

下記のコマンドでリポジトリをダウンロードします。

$ git clone https://github.com/google/EarlGrey.git

リポジトリのトップに入るとDemoというフォルダを確認できます。

$ cd EarlGrey/
$ ls -la
total 136
drwxr-xr-x  18 hirauchishinichi  staff    612  3 12 09:07 .
drwxr-xr-x   5 hirauchishinichi  staff    170  3 12 09:07 ..
drwxr-xr-x  13 hirauchishinichi  staff    442  3 12 09:07 .git
-rw-r--r--   1 hirauchishinichi  staff     23  3 12 09:07 .gitignore
-rw-r--r--   1 hirauchishinichi  staff   1273  3 12 09:07 .travis.yml
-rw-r--r--   1 hirauchishinichi  staff    144  3 12 09:07 CHANGELOG.md
-rw-r--r--   1 hirauchishinichi  staff   4207  3 12 09:07 CONTRIBUTING.md
drwxr-xr-x   3 hirauchishinichi  staff    102  3 12 09:07 Demo
drwxr-xr-x  16 hirauchishinichi  staff    544  3 12 09:07 EarlGrey
-rw-r--r--   1 hirauchishinichi  staff   1253  3 12 09:07 EarlGrey-Info.plist
-rw-r--r--   1 hirauchishinichi  staff    833  3 12 09:07 EarlGrey.pch
-rw-r--r--   1 hirauchishinichi  staff   2514  3 12 09:07 EarlGrey.podspec.json
drwxr-xr-x   4 hirauchishinichi  staff    136  3 12 09:07 EarlGrey.xcodeproj
-rw-r--r--   1 hirauchishinichi  staff  29977  3 12 09:07 LICENSE
-rw-r--r--   1 hirauchishinichi  staff   4072  3 12 09:07 README.md
drwxr-xr-x   5 hirauchishinichi  staff    170  3 12 09:07 Scripts
drwxr-xr-x   4 hirauchishinichi  staff    136  3 12 09:07 Tests
drwxr-xr-x  10 hirauchishinichi  staff    340  3 12 09:07 docs

(2) Cocoapods

Demoの中のEarlGreyExampleまで降ります。

$ cd Demo/EarlGreyExample/

そこには、Podfileconfigure_earlgrey_pods.rbがあります。

EarlGreyをCocoapodsでセットアップするとき、この2つのファイルが必要になります。

$ ls -la
total 120
drwxr-xr-x  14 hirauchishinichi  staff    476  3 12 09:21 .
drwxr-xr-x   3 hirauchishinichi  staff    102  3 12 09:21 ..
-rw-r--r--   1 hirauchishinichi  staff     56  3 12 09:21 .gitignore
drwxr-xr-x   4 hirauchishinichi  staff    136  3 12 09:21 EarlGreyExample
drwxr-xr-x   4 hirauchishinichi  staff    136  3 12 09:21 EarlGreyExample.xcodeproj
drwxr-xr-x   5 hirauchishinichi  staff    170  3 12 09:21 EarlGreyExampleSwift
drwxr-xr-x   6 hirauchishinichi  staff    204  3 12 09:21 EarlGreyExampleSwiftTests
drwxr-xr-x   3 hirauchishinichi  staff    102  3 12 09:21 EarlGreyExampleTests
drwxr-xr-x   6 hirauchishinichi  staff    204  3 12 09:21 Images.xcassets
-rw-r--r--   1 hirauchishinichi  staff  29977  3 12 09:21 LICENSE
-rw-r--r--   1 hirauchishinichi  staff   2014  3 12 09:21 LaunchScreen.xib
-rw-r--r--   1 hirauchishinichi  staff   1499  3 12 09:21 Podfile
-rw-r--r--   1 hirauchishinichi  staff    602  3 12 09:21 README.md
-rw-r--r--   1 hirauchishinichi  staff   9106  3 12 09:21 configure_earlgrey_pods.rb

Podfileの中を見ると、projectとかinherit!のコマンドが書かれていますが、これは、CocoaPods 1.0.0 beta releasesのものですので、もしお使いのCocoapodsが最新Stableの0.39.0などの場合、この行を削除しないとpod installできません。

platform :ios, '8.0'

source 'https://github.com/CocoaPods/Specs.git'

PROJECT_NAME = 'EarlGreyExample'
TEST_TARGET = 'EarlGreyExampleTests'
SCHEME_FILE = 'EarlGreyExampleTests.xcscheme'
TEST_TARGET_SWIFT = 'EarlGreyExampleSwiftTests'
SCHEME_FILE_SWIFT = 'EarlGreyExampleSwiftTests.xcscheme'

target TEST_TARGET do
  project PROJECT_NAME

  inherit! :search_paths
  pod 'EarlGrey'
end

target TEST_TARGET_SWIFT do
  project PROJECT_NAME

  inherit! :search_paths
  pod 'EarlGrey'
end

post_install do |installer|
  load('configure_earlgrey_pods.rb')
  # For each test target, you need to call the EarlGrey script's edit method once.
  # For the 'EarlGreyExampleTests' target.
  configure_for_earlgrey(installer, PROJECT_NAME, TEST_TARGET, SCHEME_FILE)
  # For the 'EarlGreyExampleSwiftTests' target.
  configure_for_earlgrey(installer, PROJECT_NAME, TEST_TARGET_SWIFT, SCHEME_FILE_SWIFT)
end

pod installを実行します。Cocoapodsは0.39.0を使用するため、Podfileの先の行は削除しました。

$ pod --version
0.39.0
$ pod install

CocoaPods 1.0.0.beta.5 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.

Analyzing dependencies
Downloading dependencies
Installing EarlGrey (1.0.0)
Generating Pods project
Checking and Updating EarlGreyExample for EarlGrey.
Adding EarlGrey Framework Location as an Environment Variable 
EarlGrey setup complete. You can use the Test Target : EarlGreyExampleTests for EarlGrey testing.
Checking and Updating EarlGreyExample for EarlGrey.
Adding EarlGrey Framework Location as an Environment Variable 
EarlGrey setup complete. You can use the Test Target : EarlGreyExampleSwiftTests for EarlGrey testing.
Integrating client project

[!] Please close any current Xcode sessions and use `EarlGreyExample.xcworkspace` for this project from now on.
Sending stats
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

(3) Xcode

pod installで作成された、ワークスペース(EarlGreyExample.xcworkspace)をXcodeで開くと、次のようにPodsプロジェクトの中に、EarlGrey.frameworkが入っていることを確認できます。

002

また、次の3つのスキームが定義されていることを確認できます。

  • EralGrayExampleSwift [テストのターゲット(Swift)]
  • EralGrayExampleTests [Objectiv-Cによるテスト]
  • EralGrayExampleSwiftTests [Swiftによるテスト]

003

まずは、スキームをEralGrayExampleSwift(テスト対象のターゲット)にセットして、動作を確認してみます。

004

(4) テスト

いよいよ、テスト実行です。

スキームをEralGrayExampleTests(Objectiv-Cによるテスト)に変更して、⌘+Uでテスト実行します。

動作している様子は、次の通りです。

3 オブジェクトの識別

テストのコードをざっと見てみると、操作対象のコントロールをgrey_accessibilityID(@"名前?")のような感じで指定しているようです。

 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"ClickMe")]
      performAction:grey_tap()];

この「ClickMe」は、何を指定しているのか?ということで、ターゲットの方を見てみると、次のようになっていました。。

let clickMe = createButton("ClickMe")

accessibilityIdentifieraccessibilityLabelに、それが設定されています。 どうやら、この識別子の指定が必要なようです。

func createButton(title: String) -> UIButton {
    let button   = UIButton(type: UIButtonType.System) as UIButton
    button.frame = CGRectMake(0, 0, 100, 100)
    button.backgroundColor = UIColor.greenColor()
    button.setTitle(title, forState: UIControlState.Normal)
    button.addTarget(self, action: "buttonAction:",
        forControlEvents: UIControlEvents.TouchUpInside)
    button.accessibilityIdentifier = title
    button.accessibilityLabel = title
    button.translatesAutoresizingMaskIntoConstraints = false
    return button;
 }

4 最後に

とりあえず、今回は、デモを動作させるところまで試してみました。

既存のプロジェクトにCocoapodsで簡単に取り込め、Objective-CでもSwiftでもテストコードが書けるようなので、これから、色々触ってみたいと思います。

5 参考資料


[iOS] UI自動化テストフレームワークEarlGreyでテストを書いてみた
EarlGrey - iOS 向けの UI 機能テスト フレームワーク
https://github.com/google/EarlGrey
iOS UIテストフレームワーク EarlGrey のセットアップ
http://www.testingexcellence.com/earlgrey-open-source-test-automation-tool-for-ios/
https://kazucocoa.wordpress.com/2016/02/17/ios-test-ui-with-googleearlgrey/