Configuring Gradle Test tasks for Android to log events - android

When I execute Android tests on the command-line using Gradle, I would like to see test log output. I followed the suggestion in this post but events are not being logged.
My app/build.gradle:
apply plugin: 'com.android.application'
...
//Test Logging
tasks.withType(Test) {
testLogging {
events "started", "passed", "skipped", "failed"
}
}
My project has multiple product flavors (free, paid) and build types (debug, release). I expected this would configure these tasks for test logging:
connectedAndroidTestPaidDebug
connectedAndroidTestFreeDebug
connectedAndroidTestPaidRelease
...
In Android Studio, I see warnings:
Cannot resolve symbol 'testLogging'
Cannot resolve symbol 'events'

have you tried using :
testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}

Related

FirebaseAppDistribution: Missing app id in the appDistributionUpload gradle command

I am using the Firebase App Distribution with the Gradle plugin.
When I try to run the appDistributionUploadDebug command I receive the error:
Getting appId from output of google services plugin
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:appDistributionUploadDebug'.
> Missing app id
I included the plugin in the build.gradle file
dependencies {
classpath 'com.google.firebase:firebase-appdistribution-gradle:1.2.0'
}
Then I added the config:
apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.appdistribution'
android {
//...
debug {
firebaseAppDistribution {
serviceAccountCredentials = "/path/to/your-service-account-key.json"
releaseNotesFile="/path/to/releasenotes.txt"
testers="email#example.com"
}
}
}
If you are not using the google services gradle plugin in your project you have to add the appId property in your configuration.
Something like:
firebaseAppDistribution {
// Firebase App Distribution setup
appId = "...." //<-- add your appID
serviceAccountCredentials = "/path/to/your-service-account-key.json"
releaseNotesFile="/path/to/releasenotes.txt"
testers="email#example.com"
}
You can find the appId value in the google-services.json file
{
...
"client": [
{
"client_info": {
"mobilesdk_app_id": "...",//<-- YOU NEED THIS APP_ID
"android_client_info": {
"package_name": "..."
}
},
...
}
or in the Firebase console on the Project Settings page -> General Tab.
Distribute Android apps to testers using Gradle
Output error on appDistributionVersion 2.0.0:
Could not find the APK. Make sure you build first by running ./gradlew
assemble[Variant]
So that the APK can be found, make sure you build first by running ./gradlew assemble[Variant] and then ./gradlew appDistributionUpload[Variant].
$ ./gradlew assemble[Variant] appDistributionUpload[Variant]

Could not find method test() for arguments on Android studio project

In my Android application, I want exclude some test cases in a package so that I used test task in build.gradle file. for example:
apply plugin: 'com.android.library'
test{
exclude '**/calltest/Summary.class'
}
If sync the project I got following exception:
* What went wrong:
A problem occurred evaluating project ':SdkModule'.
> Could not find method test() for arguments [build_4g3vf7b615x3x1p7i9ty0pt1l$_run_closure1#73d026ca] on project ':SdkModule' of type org.gradle.api.Project.
If I add apply plugin : 'java'
CONFIGURE FAILED in 1s
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
Please help me on this.
Got similar problem when try to generate XML test report for my Jenkins build.
Test related settings should be in testOptions. My file:
android {
testOptions {
unitTests.includeAndroidResources = true
unitTests.all {
reports {
junitXml.enabled = true
html.enabled = false
}
}
}
Instead of
test {
exclude '**/calltest/Summary.class'
}
try in Groovy
tasks.withType(Test) {
exclude '**/calltest/Summary.class'
}
or in Kotlin DSL (build.gradle.kts)
tasks.withType<Test> {
exclude("**/calltest/Summary.class")
}

Crashlytics NDK 2.0.1 doesn't log native crashes

I'm trying to migrate Crashlytics 2.6.7 -> 2.8.0 and Crashlytics NDK 1.1.6 -> 2.0.1
But I encountered the issue that each time my app is restarted after native crash, Crashlytics writes to logcat something like this:
12-18 19:48:33.499 11678-11717/? W/CrashlyticsCore: No minidump data found in directory /data/data/com.example.app/files/.Fabric/com.crashlytics.sdk.android.crashlytics-ndk/native/1513601249792
and I don't see any info about the crash in my dashboard. At the same time, Java crashes are successfully logged and appear in the dashboard.
My build.gradle:
apply plugin: 'io.fabric'
crashlytics {
enableNdk true
}
dependencies {
compile('com.crashlytics.sdk.android:crashlytics:2.8.0#aar') {
transitive = true;
}
compile('com.crashlytics.sdk.android:crashlytics-ndk:2.0.1#aar') {
transitive = true
}
}
The C++ code I'm using for test (there is SIGSEGV should be thrown):
if(_stream)
{
delete _stream;
_stream->close();
_stream=NULL;
}
What I need to change to make it work?

Gradle Android: How to Display test results without using --info

I am running android tests using the Gradle Android plugin and want to see individual test results.
From answers to this Question Gradle: How to Display Test Results in the Console in Real Time? it seems I can either use --info (which prints a LOT of other verbose junk I don't care about) or use this closure which only works for the Java plugin (not the Android plugin)
test {
afterTest { desc, result ->
println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
}
Is there some other option / closure I can use when I am running the connectedCheck task just to print the individual test results without all the other "verbosity".
Use Gradle info
This will print all information from Gradle:
gradle --info
or Use Android Gradle plugin:
android.testOptions.unitTests.all {
// Configure whether failing tests should fail the build
ignoreFailures false
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
}
}
or Use Gradle directly:
allprojects {
tasks.withType(Test) {
testLogging {
exceptionFormat "full"
showCauses true
showExceptions true
showStackTraces true
showStandardStreams true
events = ["passed", "skipped", "failed", "standardOut", "standardError"]
}
}
}
See: https://github.com/jaredsburrows/android-gradle-java-app-template/blob/master/gradle/compile.gradle#L20
Output:
io.github.hidroh.materialistic.data.SessionManagerTest > testView PASSED
io.github.hidroh.materialistic.data.SessionManagerTest > testIsViewFalse PASSED
io.github.hidroh.materialistic.data.SessionManagerTest > testIsViewNull PASSED
io.github.hidroh.materialistic.data.SessionManagerTest > testIsViewTrue PASSED
io.github.hidroh.materialistic.data.SessionManagerTest > testViewNoId PASSED
Source: https://github.com/hidroh/materialistic/blob/master/robolectric.gradle
Gradle Docs: https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/logging/TestLogEvent.html
For me all the options mentioned in the other answer still printed a lot of verbose information. So despite the requirement that info should not be used, I successfully use the following. For example, if your tests are in the package com.example.android, you can use:
gradle --info connectedDebugAndroidTest | grep "com\.example\.android\..* >"
Will print e. g.:
com.example.android.login.LoginActivityTest > enterCredentialsTest[Testing_emulator(AVD) - 9] SUCCESS
And the word "SUCCESS" will be green, which is awesome.
For Android Studio (tested on com.android.tools.build:gradle:2.1.0 and gradle version gradle-2.10) I added the following section to print exceptions in full format as well as logging every executed test:
apply plugin: 'com.android.application'
android { ... }
dependencies { ...}
tasks.withType(Test) {
testLogging {
exceptionFormat "full"
}
afterTest { desc, result ->
println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
}

How I can display Log files,System.out.println(), In Android test?

I did search a lot but unfortunately couldn't get it to work.
Based on my search I found that I need to add following code into build.gradle file. However, Gradle seems doesn't recognize it and always says Geadle DSL method not found: test()
test {
testLogging.showStandardStreams = true
testLogging.events("passed", "skipped", "failed", "standardOut", "standardError")
afterTest { desc, result ->
println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
}
Update
I can confirm above code or better than that following code is working fine if you create a test project and move all your test cases inside that instead of src/test/java and src/androidTest/java in main project. It's because you can apply java plugin in build.gradle file. However, It's not possible to use following code in any build.gradle file that com.android.* has been defined. Since these two libraries are not compatible :(
apply plugin: 'java'
evaluationDependsOn(':YOUR-LIB')
test {
testLogging.showStandardStreams = true
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
exceptionFormat = 'full'
}
afterTest { desc, result ->
println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
forkEvery = 5
maxParallelForks = java.lang.Runtime.runtime.availableProcessors() / 2
}
tasks.withType(Test) {
// Need Gradle to ignore classes that are inner class of Test classes but not actually Tests
scanForTestClasses = false
include "**/*Test.class"
}
So, my question is does anyone's INVENTED any way to print out logs under android plugin?

Categories

Resources