2011年4月14日木曜日

JUnitによるUIテスト - UI test by JUnit -

このエントリーをはてなブックマークに追加
Androidのテストフレームワークでは、ユーザインタフェース機能のテストができるようです。

In the Android testing framework, it seems to be able to test the User Interface function.


ActivityInstrumentationTestCase2クラスを利用すると、例えばボタンを押下したときのテストを行うことができるようです。そのテストの行い方とテストコードについてまとめました。


テスト対象のアプリ
ボタンを押下すると数字が0から1ずつカウントされるアプリを作りました。ただし、9の次は0に戻ります。

/SGL/res/layout/main.xml






/SGL/src/sgl/example/counter/Counter.java
package sgl.example.counter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class Counter extends Activity implements OnClickListener{
     private int counter = 0;
     TextView counterText;
     View counterButton;

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        counterText = (TextView) this.findViewById(R.id.counter_text);
        counterText.setText("0");
        counterButton = this.findViewById(R.id.counter_button);
        counterButton.setOnClickListener(this);
    }

     public int counter() {
          counter++;
          if (counter >= 10) {
               counter = 0;
          }
          return counter;
     }

     public void onClick(View v){
          counterText.setText(Integer.toString(counter()));
     }
}



テストプロジェクトの作成
1. eclipseの[ファイル]→[新規]→[その他]をクリック。
[新規]画面が開かれる。

2. [Android Test Project]を選択して[次へ]ボタンをクリック。
[新規 Android テスト・プロジェクト]画面に遷移する。

3. 次のように値を設定して[完了]ボタンをクリック。
・[Test Project Name:]に「SGLTest」を入力
・[コンテンツ]
- [デフォルト・ロケーションを使用]にチェック
- [ロケーション:]に「C:/eclipse/workspace/SGLTest」を入力
・[Test Target]
- [An existing Android project]にチェックを入れ、そのフォームに「SGL」を入力
・[ビルド・ターゲット]の[ターゲット名]が[Android 2.2]の項目にチェック
・[プロパティー]
- [アクション名:]に「SGLTest」を入力
- [パッケージ名:]に「sgl.example.counter.test」を入力

また、Androidの単体テストには、テストプロジェクトのAndroidManifest.xmlでを指定する必要があります。上記手順でテストプロジェクトを作成すると、このタグがデフォルトで入っている思います。


テストコード
次のようなテストを想定しました。
・最初は0が表示される
・0が表示された状態でボタンを押下すると1が表示される
・1が表示された状態でボタンを8回押下すると9が表示される
・9が表示された状態でボタンを押下すると0が表示される

テストは、テスト対象アプリのボタン押下を行うスレッドを作成し、それを利用して行います。ボタン押下を行うスレッドはrunOnUiThreadを利用して作成します。このメソッドの引数にボタン押下機能(performClick)を実装したRunnableを指定します。performClickはViewのOnClickListenerを呼び出すメソッドです。

runOnUiThreadはcountメソッドで呼び出しており、countの最後の方でwaitForIdleSyncを呼び出しています。waitForIdleSyncはアイドル状態のアプリケーションを待つメソッドで、ボタン押下が終わるまで待たせるようです。

/SGLTest/src/sgl/example/counter/test/CounterTest.java

package sgl.example.counter.test;

import sgl.example.counter.R;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.widget.TextView;
import sgl.example.counter.Counter;

public class CounterTest extends ActivityInstrumentationTestCase2 {

     private TextView counterText;
     private View counterButton;
     private Activity activity;

     public CounterTest() {
          super("sgl.example.counter", Counter.class);
     }

     @Override
     protected void setUp() throws Exception {
          super.setUp();
          activity = this.getActivity();
          counterText = (TextView)activity.findViewById(R.id.counter_text);
          counterButton = activity.findViewById(R.id.counter_button);
     }

     @Override
     protected void tearDown() throws Exception {
          super.tearDown();
     }

     private void count() {
          activity.runOnUiThread(new Runnable() {
               public void run() {
                    counterButton.performClick();
               }
          });
          this.getInstrumentation().waitForIdleSync();
     }

     public void testCount() throws Exception {
          assertEquals("0", counterText.getText().toString());
          count();
          assertEquals("1", counterText.getText().toString());
          count();
          count();
          count();
          count();
          count();
          count();
          count();
          count();
          assertEquals("9", counterText.getText().toString());
          count();
          assertEquals("0", counterText.getText().toString());
     }
}


実行
[プロジェクト・エクスプローラ]の[SGLTest]を選択した状態で右クリック→[実行]→[Android JUnit Test]をクリックするとテストが実行されます。実行結果はグリーンでした。

1 件のコメント:

  1. こんにちは。

    uiのテストができるのは助かりますね!

    返信削除