0

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..

Adeel Turk
  • 897
  • 8
  • 23

2 Answers2

4

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 of staging build
  • src/testFull - tests to run for all builds with full flavor
  • src/androidTestTrialDebug - tests to run on only trial flavor of debug build
  • src/testFullStaging - tests to run on only full flavor of stagins 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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    Thanks 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
  • Now that is a perfect answer.. Hope it help someone else – Adeel Turk Apr 27 '18 at 14:17
  • @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
0

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.

Adeel Turk
  • 897
  • 8
  • 23