Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Instrumentation testing build variant

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.

like image 923
MrEngineer13 Avatar asked Sep 09 '15 16:09

MrEngineer13


People also ask

What is build variant in Android Studio?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.

What are Android instrumentation tests?

Instrumented tests are tests that run on physical devices and emulators, and they can take advantage of the Android framework APIs and supporting APIs, such as AndroidX Test.

What are build types in Android?

Once the new project is created, by default it consists of two build types/variants - debug, release. Debug is the build type that is used when we run the application from the IDE directly onto a device. A release is the build type that requires you to sign the APK.


2 Answers

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" } 
like image 107
sunilr Avatar answered Sep 18 '22 22:09

sunilr


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  
like image 25
shizhen Avatar answered Sep 17 '22 22:09

shizhen