Android gradle test only one flavor - android

I've currently set my TeamCity instance to run connectedCheck on all my Android-projects. This is fine, all tests run and everything is good. Except, connectedCheck runs all tests for all product flavors. I currently have a lot of flavors, so this is wasted work for my projects as I do not really have any different code in the flavors. Any idea how I can make connectedCheck only run for one flavor?

./gradlew connected[Flavor]DebugAndroidTest

./gradlew connectedBuildVariantAndroidTest
Example:
./gradlew connectedDevelopmentDebugAndroidTest

As vida said you can run ./gradlew tasks to check all possibilities to run gradle commands.
I'd like to suggest the following:
./gradlew tasks | grep connected.
This filter all connectedAndroidTest variants that can be runned.

I see there is a new task since I checked this the last time, "connectedAndroidTestProductFalvor_buildvariant".
I haven't had time to check this task yet, but the description reads "Installs and runs the tests for build ProductFlavor_buildvariant on connected devices."
Will post a result when I've been able to test this.

and if you want to generate the apk for the android test :
./gradlew assemble[flavour]DebugAndroidTest

Related

NoClassDefFoundError when running unit test with Gradle task

I have a project using dynamic feature module, and I want to run my unit test in feature module via gradle task (for my CI purpose):
./gradlew :feature_product:test
But it always gives me NoClassDefFoundError for tests that have dependencies on classes from the base module:
com.example.android.feature.product.ProductViewTest > on vote change to negative FAILED
java.lang.NoClassDefFoundError: app.BaseView
ProductView class from the feature module extends BaseView from the base module.
Oddly, it succeeds when run in Android Studio, it works fine.
Then I notice something different in the logs, when I run via command line and when I run Android Studio. The first line in the Android Studio is generateDebugSources, something which absent when I run ./gradlew test
Executing tasks: [:lib_ui:generateDebugSources, ...]
How do I fix this? Does Android Studio has different command with the provided command ./gradlew test when I press Ctrl+Shift+R ?
After searching further about this issue, I found it also being reported in the android-test and app-bundle-samples projects and there is also an issue in the issue tracker.
It turns out this issue fixed in the Android Gradle Plugin 4.1.0 as per comment in the issue tracker.
If you don't want to update AGP to 4.1.0 which is still in alpha, adding this to the feature module's build.gradle fixed the issue for me, as per this comment:
testRuntimeOnly(files("$projectDir/../b_app/build/intermediates/app_classes/debug/classes.jar"))
If it is a missing task that you believe is necessary then calling it first like below should do the trick:
./gradlew :lib_ui:generateDebugSources :feature_product:test
I would even go full on and assemble the dependencies if necessary though that might take more time:
./gradlew :lib_ui:assemble :feature_product:assemble :feature_product:test

Android Instrumentation test does not test Release build

I am currently testing my sample app using Android instrumentation test. By default, the project creates AndroidTest folder for me. I simply just added more test cases into the folder.
I used to use expresso to trigger the UI buttons, but now I want to test use androidTest only, However, the androidTest does not seem to test my release build. I have two variants productionRelease and stageDebug in this case.
Every time I started the project by
./gradlew mysample:connectedCheck
or
./gradlew mysample:connectedAndroidTest
it tests only
Task :mysample:connectedStageDebugAndroidTest
If I want to manually start a task
./gradlew mysample:connectedProductionReleaeAndroidCheck
It complains tasks not found in mysample
* What went wrong:
Task 'connectedProductionReleaseAndroidTest' not found in project ':mysample'.
Isn't connectCheck supposed to test all the variants in my project? (StageDebug and ProductionRelease)
from task --all
mysample:connectedCheck - Runs all device checks on currently connected devices.
mysample:connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
Just tried... one can simply run ./gradlew mysample:testDebugUnitTest and ./gradlew mysample:testReleaseUnitTest - which runs the tests for either debug or release build. one can add the annotation #RequiresDevice to tests, in case eg. one requires hardware sensors.
productionRelease and stageDebug seem over-complicated (and it also not matches the rest of the naming - unless there would be productionDebug and stageRelease as well), resulting in even longer task names, would suggest to shorten...
buildTypes {
debug {}
release {}
}
Here when I run ./gradlew mysample:connectedAndroidTest I rather get a
Execution failed for task ':mysample:connectedDebugAndroidTest'.
com.android.builder.testing.api.DeviceException: No connected devices!
After closing Android Studio (and the ADB daemon it started), I can run the tests on hardware device. there only is a installDebugAndroidTest task, but no installReleaseAndroidTest task. testBuildType 'release' might run the tests against the release build - the problem is just that androidTestImplementation most likely is not contained then (quite useless).

Are Android gradle tasks open sourced?

I use ./gradlew connectedAndroidtest to test my android app.
When the connectedAndroidtest task running, from the terminal, I can get the task that ran many sub-tasks.
:assembleDebugAndroidTest UP-TO-DATE
:connectedDebugAndroidTest ...
but I don't understand the sub-tasks details.
I try to find gradle source code but can't find any about connectedDebugAndroidTest tasks.
Are android tasks open source? Or where I can know more details?
Thanks.
If you would like to see Android Build Tools source code, there is open Google repository with it: android/platform/tools/build/master
Specifically Android Gradle Plugin: build/gradle.
If you would like to see manual for specific task, you could execute:
./gradlew help --task "${taskName}"
In your case it should be:
./gradlew help --task connectedAndroidTest
Output:
Detailed task information for connectedAndroidTest
Path
:app:connectedAndroidTest
Type
Task (org.gradle.api.Task)
Description
Installs and runs instrumentation tests for all flavors on connected devices.
Group
verification

Incremental Instrumented Android Test Results

I am using TeamCity and I am looking at how to get incremental instrumented android test results.
To run the tests I run ./gradlew :TestApp:connectedAndroidDebugTest --info
This gives me output of what tests are running and their state but the full report is only generated at the end.
How can I get incremental android test results in TeamCity? I can add custom logs that TeamCity will pick up, but I am unsure where to add them in the gradle build file.
Looks like, you are using Command Line runner step.
Try using Gradle runner step instead. You will get on-the-fly test reporting with it. TeamCity will display each test as soon as it is executed.

Android build variants on travis.ci

I currently have an Android project using gradle and integrated with travis.ci which has different productFlavors and buildTypes. When the "connectedCheck" task is then executed on travis.ci, it tries to package all build variants (all combinations of flavors and types). Some of them fail as the release builds need password input which I can't automate at the moment. Is there a way to tell travis.ci to build and test only a certain build variant of an Android project?
Say you only want to run the product flavor Trial and the build type Debug.
Instead of running ./gradlew assemble connectedCheck, which is similar to what you're doing, run this instead:
./gradlew assembleTrialDebug connectedCheckTrialDebug
So here's how I made it work:
Run a connectedAndroidTest<productFlavor><buildType> task instead of connectedCheck.
Also set the assemble task in the install section of the .travis.yml:
install: - TERM=dumb ./gradlew -s assemble<productFlavor><buildType>

Categories

Resources