What does the "Test Artifact" selector do in Android STudio

3.1k Views Asked by At

What does the test artifact selector actually do? For some reason when I set it to Unit Tests, Android Studio (AS) is able to recognize and run my Junit 4 tests. When I set it to Android Instrumentation Tests, JUnit 4 disappears from my class path and AS marks the code as incorrect (cannot resolve symbol junit for import org.junit.Test).

Is there some way to tell AS to treat a selection of class files as JUnit tests that I don't want to run inside of the emulator? Is this on their radar at all? It seems crazy that I have to flip this switch to move from JUnit tests to integration-y tests, and it seems to affect the entire project, not just one build/release flavor, or just one subdirectory of my project.

Here's my build.gradle, using gradle 2.2.1, canary channel of Android Studio 1.3 (AI-141.1972460), Android SDK Tools 24.3.0

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}
allprojects {
    repositories {
        jcenter()
    }
}

// Manifest version information!
def versionMajor = 0
def versionMinor = 1
def versionPatch = 0
def versionBuild = 1 // bump for dogfood builds, public betas, etc.apply plugin: 'android'

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }

}

def gitSha = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()
def buildTime = new Date().format("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC"))

android {
    dexOptions {
        preDexLibraries = false
    }

    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 22

        multiDexEnabled true
        testInstrumentationRunner "android.support.multidex.MultiDexTestRunner"

        versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
        versionName "${versionMajor}.${versionMinor}.${versionPatch}"

        buildConfigField "String", "GIT_SHA", "\"${gitSha}\""
        buildConfigField "String", "BUILD_TIME", "\"${buildTime}\""
    }

    productFlavors {
        // Define separate dev and prod product flavors.
        dev {
            // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
            // to pre-dex each module and produce an APK that can be tested on
            // Android Lollipop without time consuming dex merging processes.
            minSdkVersion 21
        }
        prod {
            // The actual minSdkVersion for the application.
            minSdkVersion 14
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix '.dev'
            versionNameSuffix '-dev'
        }

        release {

        }
    }

    lintOptions {
        abortOnError false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'LICENSE.txt'
    }

    sourceSets {
        instrumentTest.setRoot('src/test')
        androidTest.setRoot('src/test')
    }
}

dependencies {
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.google.android.gms:play-services-base:7.5.0'
    compile 'com.google.android.gms:play-services-plus:7.5.0'
    compile 'com.google.android.gms:play-services-identity:7.5.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:cardview-v7:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.android.support:support-annotations:22.2.0'
    compile 'com.squareup.dagger:dagger:1.2.2'
    provided 'com.squareup.dagger:dagger-compiler:1.2.2'
    compile 'com.squareup.okhttp:okhttp:2.3.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.jakewharton:butterknife:6.1.0'
    compile 'com.jakewharton.timber:timber:3.0.1'
    compile 'io.reactivex:rxjava:1.0.9'
    compile 'io.reactivex:rxandroid:0.24.0'
    compile 'io.reactivex:rxandroid-framework:0.24.0'
    compile 'com.github.matthewyork:ColoursLibrary:1.0.0@aar'
    compile 'com.nispok:snackbar:2.10.7'
    compile 'com.github.frankiesardo:icepick:2.3.6'
    provided 'com.github.frankiesardo:icepick-processor:2.3.6'
    compile 'nl.qbusict:cupboard:2.1.1'
    compile 'org.lucasr.twowayview:core:1.0.0-SNAPSHOT@aar'
    compile 'org.lucasr.twowayview:layouts:1.0.0-SNAPSHOT@aar'
    compile 'it.neokree:MaterialNavigationDrawer:1.3.3'

    testCompile 'junit:junit:4.12'
}

Build Variants Test Artifact selector in Android Studio

1

There are 1 best solutions below

1
On BEST ANSWER

Now you can remove the lines

sourceSets {
    instrumentTest.setRoot('src/test')
    androidTest.setRoot('src/test')
}

and instead keep the Instrumentation tests in /src/androidTest and Junit tests (not depending on android api's) in src/test

You will have to provide dependencies for the instrumentation android tests and non android test seperately. For android tests use androidTestCompile and for junit test use testCompile

dependencies {
  ...
  testCompile 'junit:junit:${junitVersion}'
  testCompile 'org.mockito:mockito-core:${mockitoVersion}'

  androidTestCompile 'junit:junit:${junitVersion}'
  androidTestCompile 'org.mockito:mockito-core:${mockitoVersion}'
  androidTestCompile 'com.android.support.test.espresso:espresso-core:${espressoVersion}'
}

Giving these dependencies separately will remove the issue as experienced by you in the question. more details here http://tools.android.com/tech-docs/unit-testing-support