I have a multiple falvoured and multiple build type project..having some espresso and unit test cases which are running successfully but to run them successfully i have mocked my api responses currently i change my build type manually in android studio to stagging and then run those tests. Now i am integrating my project with jenkins and in jenkins my tests failed becuase jenkins is running test cases on all build types.. So long story Short I want my test cases to run only using staging build type..
-
I can't provide an answer without more information from you, but I think you can likely do what you want by following my answer here: https://stackoverflow.com/questions/31819619/run-espresso-tests-after-proguard/32751358#32751358 – jdonmoyer Apr 26 '18 at 21:05
-
@jdonmoyer i did that already but it didnt worked – Adeel Turk Apr 27 '18 at 09:43
-
Thanks for your patience. I answered with more details than my earlier comment. – Code-Apprentice Apr 27 '18 at 13:58
-
@Code-Apprentice Pleasure is mine.. :) – Adeel Turk Apr 27 '18 at 14:11
2 Answers
As stated in android test support flavors and build type variants?, you can create androidTest
and test
folders for specific flavors. You can also extend this to build types. Gradle allows you to build tests for any specific combination of build-type and flavor.
Let's say you have two build types (staging
and debug
) and two flavors (trial
and full
). All of the following are valid folder names:
src/androidTestStaging
- tests to run for all flavors ofstaging
buildsrc/testFull
- tests to run for all builds withfull
flavorsrc/androidTestTrialDebug
- tests to run on onlytrial
flavor ofdebug
buildsrc/testFullStaging
- tests to run on onlyfull
flavor ofstagins
build
Each of these folders have the exact same directory structure as androidTest
and test
respectively. The tests in each folder will only be run against a build which is made from all of the specified build types and flavors.
Note that this can quickly explode into a lot of combinations. For example, if you have 3 build types and 2 flavor dimensions with 3 flavors each, you now have 27 different builds. Maintaining tests for each of these can be a nightmare, so you should still follow standard software engineering practices to write tests and helper classes and functions that can be reused as much as possible.

- 81,660
- 23
- 145
- 268
-
1Thanks alot @Code-Apprentice .. I want to add one more thing here.. I have multiple flavours and multiple build types , so with the help of your answer on the comments here https://stackoverflow.com/questions/48895869/android-test-support-flavors-and-build-type-variants/48895920 .. I ended up making dir's like testFlavourARelease , testFlavourADebug,testFlavourAStagging (for unit test) and same would apply on espresso one's accordingly.. – Adeel Turk Apr 27 '18 at 14:11
-
-
@Code-Apprentice I've tried your solution but sources for androidTest
are not recognized by android studio. If you'll be so kind and check my issue which is closely related to this problem. https://stackoverflow.com/questions/56112541/separate-androidtest-sourceset-for-different-buildtype-not-build-flavor – xyman May 13 '19 at 15:01 -
@Code-Apprentice you should correct your answer, it is not possible to create separate androidTest folders for every build-type. That's only possible for build `flavors` and `exclusively` for debug build-type(androidTestDebug)! Eg. if you have this build types: debug, release and integration you can't create androidTestIntegration directory for integration build-type instrumentation tests, the only one you can add is androidTestDebug. By executing `./gradlew sourceSets` you can see which sourceSets are allowed. – xyman May 30 '19 at 14:05
-
2@Code-Apprentice unfortunately there is nothing about this issue in docs i came to that coclunsion by testing - am not sure if that's a bug or feature. You can try it in fresh project. The most important lead to me was `./gradlew sourceSets` which doesnt list `androidTestIntegration` sourceSet and on top of that it looks like android gradle plugin have dropped support for adding new custom sourceSets: `https://stackoverflow.com/questions/49601232/the-sourceset-commontest-is-not-recognized-by-the-android-gradle-plugin-perha#comment99352101_49601232` . – xyman May 30 '19 at 14:57
Answered by Code-apprentice on this question
If you want to run tests against a specific build type, you can specify it in the folder name: src/androidTestStaging or src/testStaging. You can combine build types and flavors to run specific tests: src/androidTestTrialStaging or src/testFullDebug.

- 897
- 8
- 23