36

Is there any way to turn on the test logging in the console output?

I know that we can look at the test results generated in a HTML file and check standard output there, but I find it a little bit inconvinient.

I know that there is a way to do this with standard java plugin:

test {
    testLogging {
        events "passed", "skipped", "failed", "standardOut", "standardError"
    }
}

But using it in an Android project causes an error:

Could not find method test()

Applying java plugin is unacceptable, of course, as it's not compatible with Android plugins.

scana
  • 2,785
  • 2
  • 25
  • 49
  • Which console do you mean? All logs are came from device. `Logcat` tool fetches all logs. – eleven Mar 03 '15 at 12:46
  • Sorry, maybe I didn't express myself clear enough. I meant Android unit tests performed on JVM, started from simple terminal via gradle wrapper (so logcat is out of question) – scana Mar 03 '15 at 12:58

3 Answers3

53
android {

...

  testOptions {
        unitTests.all {
        // All the usual Gradle options.
            testLogging {
                events "passed", "skipped", "failed", "standardOut", "standardError"
                outputs.upToDateWhen {false}
                showStandardStreams = true
            }
        }
    } 
}

In my case, I followed this document and added the testLogging option as above. This should printout the log for the unit tests written under src/test folder but not the src/androidTest one. At the moment of this answer, I was using Android Studio 2.0 preview and gradle 2.8. The commands were ./gradlew test and ./gradlew test --continuein which ran in iTerm 2.

ninjahoahong
  • 2,624
  • 22
  • 20
  • For running the test under `src/androidTest`, I right click the package and choose `Run 'Tests in 'com.example... ` using Android Studio. – ninjahoahong Dec 15 '15 at 21:10
8

This should do it

android {

...

  testOptions.unitTests.all {
    testLogging {
      events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
    }
  }
}

Source: https://groups.google.com/forum/#!topic/adt-dev/LwZiKTnj8Bc

Kavi
  • 3,880
  • 2
  • 26
  • 23
3

Even more pretty solution than @ninjahoahong's (Thanks to Tricky Android blog).

Just add next code to your project level build.gradle to allprojects' body:

allprojects {
    // ...
    tasks.matching {it instanceof Test}.all {
        testLogging.events = ["failed", "passed", "skipped"]
    }
}
Oleksandr
  • 6,226
  • 1
  • 46
  • 54