0

for a specific project we need to run both Espresso and Robolectric test suites but it seems that their dependencies seem to clash very badly.

Therefore my question, is it even possible to have them both or should we settle for another solution?

Our Gradle file:

apply plugin: 'com.android.application'
apply plugin: 'jacoco'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"

    defaultConfig {
        targetSdkVersion 24
        minSdkVersion 15
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        debug {
            testCoverageEnabled true
        }
    }    
}

dependencies {
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'

    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'
    androidTestCompile 'junit:junit:4.12'

    androidTestCompile "org.robolectric:robolectric:3.1.4"

    compile ('com.github.nkzawa:socket.io-client:0.3.0')
}

Thanks in advance.

qantik
  • 1,067
  • 1
  • 10
  • 20
  • Have you take a look at this answer: http://stackoverflow.com/questions/29021331/confused-about-testcompile-and-androidtestcompile-in-android-gradle ? Also, according to documentation: http://robolectric.org/getting-started/ you need to use testCompile for Robolectric instead of androidTestCompile. – Fedor Tsyganov Nov 02 '16 at 20:58
  • I tried it with `testCompile` but it results in the same. It just wouldn't compile. – qantik Nov 02 '16 at 21:00
  • Can you also try to use testCompile for both JUnit and robolectric? I've tried same build.gradle file on mine machine with buildToolsVersion "24.0.3" and it compiled. – Fedor Tsyganov Nov 02 '16 at 21:14
  • Can you post exactly error that you get? – Eugen Martynov Nov 03 '16 at 17:17

1 Answers1

1

You can have them both, but you need to separate them into separate test packages.

Your robolectric tests should belong to the test package, while espresso tests should reside in the androidTest. Your dependencies will also be prefixed according to the packages (i.e robolectric dependencies will be testCompile, while espresso tests will be androidTestCompile).

This split is required due to the nature of both robolectric and espresso tests. Robolectric being a unit testing framework has android sdk fully (to some extent) mocked which allows the execution of the tests in JVM, while espresso requires "real" android for tests to be executed.

This guide from google covers the test set up in more details.

Be_Negative
  • 4,892
  • 1
  • 30
  • 33