Skip to content Skip to sidebar Skip to footer

Android Studio Project Setup For Espresso Tests

About a week ago I asked this question Why is library module android.support.test not visible in add dependency After much head scratching and project setup morphing I discovered

Solution 1:

Below is my working Espresso gradle setup based on Android Studio 1.1.0 + Espresso 2.0 + Support Library v11 + gradle environment. It looks likely that you missed androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0' library.

dependencies {
    // ...
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}

android {
    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 21
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    sourceSets {
        packagingOptions {
            //...
            exclude 'META-INF/LICENSE.txt'
            exclude 'LICENSE.txt'
        }
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['res']
        }
    }
}

About the directory, yes. just maintain androidTest and main under src folder. But, your setting does not make IDE to recognize androidTest folder as a test project folder. For that, two projects with same package name causes multi dex error I think. Use androidTestCompile to espresso-related lines in gradle dependencies and clean/rebuild project.

Note that you have to set all referenced projects gradle to same testInstrumentationRunner.

I also spent times to make it work. I wrote Korean tutorial to setup and run Espresso. Sorry about the language but screenshots might be helpful or you can try Google translate to read it.

EDIT. APPENDED BELOW

I thought why your IDE did not show green color in androidTest folder (means Test Project), and found your gradle setup seems to be wrong.

The line { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } } make IDE to recognize androidTest is a Project folder not Test Project folder. remove second one. androidTest will be automatically recognized as a Test Project.

Post a Comment for "Android Studio Project Setup For Espresso Tests"