LoginSignup
8
8

More than 5 years have passed since last update.

AndroidStudioでEspresso使ってテストするときに、つまづいた事

Posted at

個人的につまづいた事を書いていきます。
(「つまづいた」を「躓いた」と書くと自分でも読めない事に最近気づいた)

Espressoのインストール(jarファイルを入れる)は必要か?

必要なかったです。
espresso-1.1-bundled.jar
とか、せっかくダウンロードしたのに、使いませんでした。
こちらの記事だとインストールが必要なんですが、
http://qiita.com/kuchinashi_r/items/d2f2f818421da5898520
2015年にはこんな記事があります。インストールが不要になったんですね。
http://tech.cookbiz.co.jp/android-463
時代の流れを感じますね。

「build.gradle」はどこの奴を修正するのか?どう修正するのか?

正解は、
プロジェクトのルート/app/build.gradle
です。
プロジェクトのルート/build.gradle
ではありません。
dependencies に、以下の記述を追記します。

build.gradle
 // for AndroidTest
    compile 'com.android.support:support-annotations:21.0.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'

私の場合ですが、この時点でbuild.gradleは以下のようになってました。最後の方に追加したものがあります。

build.gradle
apply plugin: 'com.android.application'
apply plugin: 'android-apt'

android {
    compileSdkVersion 19
    buildToolsVersion '20.0.0'
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'LICENSE.txt'
    }

    defaultConfig {
        applicationId "com.hogehoge"
        minSdkVersion 16
        targetSdkVersion 19
        versionCode 13
        versionName "1.3"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled
        }
        debug {
            debuggable true
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
    productFlavors {
    }
    lintOptions {
        disable 'MissingTranslation'
    }
}
dependencies {
    //ここらへんは省略。

    // for AndroidTest
    compile 'com.android.support:support-annotations:21.0.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}

テストコードはどこに書けばいいのか?

私はこのように作成しました。
プロジェクトルート/app/src/androidTest/java/com.hogehoge/activity/HogeActivityTest.java

ここに、こんなソースを書きました。

HogeActivityTest.java
package com.hoge.activity;

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Created by yasuda_tadashi on 2015/08/24.
 */
@RunWith(AndroidJUnit4.class)
public class HogeActivityTest extends
        ActivityInstrumentationTestCase2<HogeActivity> {
    public HogeActivityTest() {
        super(HogeActivity.class);
    }
    @Before
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
    }

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

    @Test
    public void testHomeKey() {

    }
}

これで、一応テストコードを実行できるはず。
実行してエラーがでなければ、やっと具体的なテストを記述できる。

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