LoginSignup
120
120

More than 5 years have passed since last update.

2015年7月時点でのJUnit4やEspressoを使ったAndroidアプリのテストについて

Last updated at Posted at 2015-07-30

以前「Androidのテスティングフレームワークを選定してみる」という記事を書いたのですが、最近久しぶりにEspressoを使ったテストコードを書こうと思って調べていたら、Espressoが2.0から2.2になっていたりして、いつのまにやらTesting Support Libraryが色々と更新されているようでした。
なので、メモがてら少しまとめてみました。

testing-support-libが分割された

以下の2つに分割されたようです。

  • com.android.support.test:runner
  • com.android.support.test:rules

Espresso 2.1のReleaseNotesに破壊的な変更点として記述されています。

Breaking Changes

test runner artifact split into two and the name changed from com.android.support.test:testing-support-lib:0.1 to com.android.support.test:runner:0.2 + com.android.support.test:rules:0.2.

Espresso 2.0の頃はbuild.gradleに以下のように記述していましたが、

build.gradle
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'

Espresso 2.1からは以下のように記述します。

build.gradle
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2' 

build.gradle

Espressoを使ったテストで@Testが使えるようになった

これまでのEspresso(ver 2.0以前)でテストするには、ActivityInstrumentationTestCase2を継承する必要がありました。
そのActivityInstrumentationTestCase2はjunit.framework.TestCaseのサブクラスであり、TestCaseのサブクラスは、AndroidJUnitRunnerにJUnit3として扱われてしまうため、JUnit4の機能である@Testを使用することができませんでした。

@Test annotation is a replacement for the test-prefix naming convention used in JUnit3. JUnit4 test classes no longer require to extend TestCase or any of its subclasses. In fact JUnit4 tests cannot extend TestCase, otherwise AndroidJUnitRunner will treat them as JUnit3 tests.

User guide for AndroidJUnitRunner

ですが、Espresso 2.1でActivityTestRuleが追加され、そのActivityTestRuleによって single activityのテストが可能となったため、 ActivityInstrumentationTestCase2は不要となりました。
よって、@Testなどを使えるようになりました。

ActivityTestRuleを使ったテストコードは以下のようになります。

EspressoTest.java
@RunWith(AndroidJUnit4.class)
public class EspressoTest {

    private Activity mActivity;

    @Rule
    public ActivityTestRule<TopActivity> mActivityRule =
            new ActivityTestRule<>(TopActivity.class);

    @Before
    public void setUp(){
        // Activityの取得
        mActivity = mActivityRule.getActivity();
    }

    // @Testが使えるので日本語だけのテストメソッドが書けるようになった
    @Test
    public void 足し算機能の基本的な挙動() {
        // 数字入力
        onView(ViewMatchers.withId(R.id.num1)).perform(typeText("2"));
        onView(withId(R.id.num2)).perform(typeText("20"));

        // 足すボタンクリック
        onView(withId(R.id.equal_button)).perform(click());

        // 計算結果確認
        onView(withId(R.id.result)).check(matches(withText("22")));
    }
} 

EspressoTest.java

WebViewがサポートされた

Espresso 2.2からWebViewをテストできるようになりました。

espresso-web 2.2
New WebView support

ReleaseNotes

以下の記事で詳しく紹介されています。
Espresso webview support

さいごに

ほかにも、Intentをテストできるespresso-intentsが追加されていたりと、今回紹介した機能以外にも、便利そうな機能が色々と追加されているようです。
ReleaseNotes

120
120
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
120
120