LoginSignup
13
16

More than 5 years have passed since last update.

AndroidプロジェクトにEspressoを導入してみた

Last updated at Posted at 2016-03-19

Espressoとは

Android の UI Testing Support Library の1つです。

Androidプロジェクトへの適用

今回は、Espresso を使える状態までセットアップします。

実行環境

Ubuntu 14.04
Android Studio 1.5.1
Android SDK Tools: 24.4.1
Android SDK Platform-tools: 23.1
Android SDK Build-tools: 23.0.2
Android Support Library: 23.2.1

プロジェクトへの適用

app.gradleに追記(最小限)

app.gradle(diff)
@@ -23,4 +25,7 @@ dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     testCompile 'junit:junit:4.12'
     compile 'com.android.support:appcompat-v7:23.1.1'
+
+    // Android JUnit Runner
+    androidTestCompile 'com.android.support.test:runner:0.5'
+    // JUnit4 Rules (使わなければ不要)
+    androidTestCompile 'com.android.support.test:rules:0.5'
+    // Espresso core
+    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}

メモ (compile , testCompile , androidTestCompile)

これは何。

  • compile: プロダクトコード(src/main)に必要な依存関係
  • testCompile: テストコード(src/test)に必要な依存関係
  • androidTestCompile: Androidテストコード(src/andoridTest)に必要な依存関係

メモ ( ググると引っかかる testing-support-lib:0.1 )

これは何。

Espresso のリリースノートによると以下とのことらしい。

Espressoのリリースノートから抜粋
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.

つまり、こういうことらしい。

app.gradle(昔)
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
app.gradle(今)
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'

テストコードを書いてみる

テスト対象のActivityのXML

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="jp.chibi.espressosample.MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:name="jp.chibi.espressosample.MainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_main"/>

</RelativeLayout>

※無駄にFragmentを適用しています。

テストコード

MainActivity.java
@RunWith(AndroidJUnit4.class)
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
    }

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

    @Test
    public void test1() {
        MainActivity activity = getActivity();
        Assert.assertNotNull(activity);

        onView(withId(R.id.fragment)).check(matches(isDisplayed()));
    }

一番下の行が Espresso を用いたテストの書き方になります。
onViewwithId を渡して、 checkisDisplayed になっているかを確認。

ちなみに↓のように書くとFailします。

onView(withId(R.id.fragment)).check(matches(not(isDisplayed())));

今回は導入編ということで、ここまで。

13
16
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
13
16