8

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

Community
  • 1
  • 1
kellyfj
  • 6,586
  • 12
  • 45
  • 66
  • 1
    This is possibly with the `Robolectric Gradle Plugin` if you using `Robolectric` but this will only show the `test` and the result:`success`/`fail`. I think you best bet is you display the "full exception" for when a test fails in the console. All HTML test results are generated in your `build/reports/tests`. – Jared Burrows May 18 '15 at 18:40
  • 1
    I do not use Roboelectric - just the plain vanilla androidTests – kellyfj May 18 '15 at 19:12
  • 2
    Yeah, I believe you have it figured out. `--info` is your best bet. Adding `Robolectric` is really simply and you can just add it in order to use their plugin https://github.com/robolectric/robolectric-gradle-plugin. – Jared Burrows May 18 '15 at 19:15
  • 1
    For errors, you want this: http://stackoverflow.com/a/24388879/950427 – Jared Burrows May 18 '15 at 19:27
  • 1
    Our tests take a long time to run so i want to see the successes as well as the failures just to have confidence that nothing terrible has blown up – kellyfj May 18 '15 at 20:08
  • 1
    Does your build not fail on the first test case failure? Check out the link I posted, it will print out all failure exceptions as well as `expected`/`was`. – Jared Burrows May 18 '15 at 21:29
  • 1
    I came across something that may help answer this. – Jared Burrows Aug 02 '15 at 16:55
  • What is the status of this? – Jared Burrows Nov 20 '15 at 18:16

3 Answers3

15

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

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • Do you know how this can be applied to all gradle (library) modules in a project? Putting it in the top-level build.gradle fails as `android` isn't recognised, and putting it in the app module only prints output for tests in that module. – user1405990 Jul 07 '16 at 22:58
  • @user1405990 Use `allprojects` with `tasks.withType(Test)` – Jared Burrows Jul 07 '16 at 23:49
4

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.

Miloš Černilovský
  • 3,846
  • 1
  • 28
  • 30
2

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}"
    }
}