31

So I am trying to write instrumentation tests using a custom build variant, mock. In this build variant I mocked up my classes and server. When I try using the mock build myself it works fine, but I can't seem to use my mock build for testing. Here's what my configuration looks like inside Android Studio.

Build Variants

I had some issues getting my tests to run so I tried to uninstall all versions of my app except my mock version and I keep getting this error:

Test running startedTest running failed: Unable to find instrumentation target package: com.teamtreehouse.review.debug

However when I try to run my tests against the debug build variant it works fine. It installs my debug version then proceeds to run the tests.

MrEngineer13
  • 38,642
  • 13
  • 74
  • 93

3 Answers3

76

You can do testing on a different build variant; but only on one. The default is debug.

See this: https://developer.android.com/studio/build/gradle-tips#change-the-test-build-type

Currently only one Build Type is tested. By default it is the debug Build Type, but this can be reconfigured with:

android {
    ...
    testBuildType "staging"
}
Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
sunilr
  • 1,146
  • 10
  • 11
11

Alternatively, you can configure your testBuildType as following way so that you can decide to run any build type of the androidTest specifying the respective property from command line.

android {   
    ...

    if (project.hasProperty('androidTestRelease')) {
        testBuildType 'release'
    } else if (project.hasProperty('androidTestStaging')) {
        testBuildType 'staging'
    } else {
        testBuildType 'debug'
    }
    ...
}

From command line

./gradlew connectedCheck -PandroidTestStaging 
shizhen
  • 12,251
  • 9
  • 52
  • 88
  • 1
    I was wondering why you need to write this code instead of declaring the `testBuildType` for each flavor block. Looks like it's not supported at the moment, as Gradle always consider the last `testBuildType` (doesn't matter where it is placed). – JCarlosR Jun 06 '22 at 22:36
7

AFAIK androidTest only works against the debug buildType.

You can use build flavours to do what you want, a good example can be found here: https://www.code-labs.io/codelabs/android-testing/#0

zmarkan
  • 605
  • 5
  • 13