102

While testing, Gradle appears to redirect stdout/stderr to project_dir/build/reports/tests/index.html. Is there a way to avoid this redirection, and get things printed to the console instead?

Additional information:

  • It's a Scala 2.9.1 project.
  • I am using slf4s for logging.
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
missingfaktor
  • 90,905
  • 62
  • 285
  • 365

11 Answers11

130
apply plugin : 'java'

test {
    testLogging.showStandardStreams = true
}

http://gradle.org/docs/current/dsl/org.gradle.api.tasks.testing.Test.html

This requires a current gradle version. I am assuming that the Scala tests are run under the Java test task.

roby
  • 3,103
  • 1
  • 16
  • 14
  • 1
    Whoops on testing it looks like it doesn't, this produces output for me though: http://pastebin.com/PX8e1EKv edit: amend the println to a print as I think the event captures the newline – roby Feb 20 '12 at 09:13
  • thanks. I'll try that too. I have some log statements in source files too. How do I make them print to console? – missingfaktor Feb 20 '12 at 09:42
  • It depends on the logging implementation. slf4s might be defaulting to a NOP logger and discarding your log statement. Do you have a logger implementation jar on the test classpath? If slf4j-simple-1.6.4.jar is present it will log INFO and above to STDERR. If that isn't enough I would add a logback jar and set up a logback-test.xml to append logs to the console. – roby Feb 20 '12 at 19:31
  • Worked for me w/ Spring/JUnit – Dmitry Minkovsky Jan 09 '15 at 16:54
  • It "doesn't work" possibly because the test task is considered up to date and doesn't run hence you see no output. You can either do `$ gradle clean test` or use `outputs.upToDateWhen {false}` inside the `testLogging` block (`test { testLogging { outputs...`). For details see the [docs](https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskOutputs.html#upToDateWhen-groovy.lang.Closure-). – Johnny Baloney Feb 09 '18 at 16:12
  • @missingfaktor this works for sysouts and log.error but not for log.info() – best wishes May 23 '18 at 07:18
  • It works on Terminal but my IntelliJ doesn't show the logs during Graddle tasks running. – emeraldhieu Jun 09 '23 at 08:22
40

I am using also (testLogging.exceptionFormat = 'full'):

test {
    testLogging.showStandardStreams = true
    testLogging.exceptionFormat = 'full'
}

Which is good to see more from stacktrace

To Kra
  • 3,344
  • 3
  • 38
  • 45
27

For Android Gradle Files

If you are inside an android gradle file (if apply plugin: 'com.android.application' is at the top of your build.gradle file)

Then paste this into build.gradle

// Test Logging
tasks.withType(Test) {
    testLogging {
        events "standardOut", "started", "passed", "skipped", "failed"
    }
}

For Regular Gradle Files

Paste this into build.gradle

// Test Logging
test {
    testLogging {
        showStandardStreams = true
    }
}
Community
  • 1
  • 1
joshuakcockrell
  • 5,200
  • 2
  • 34
  • 47
22

As @roby answered:

adding the following code to your build.gradle

apply plugin : 'java'

test {
    testLogging.showStandardStreams = true
}

Important!

You need to run gradle test or build with added clean command.

./gradlew clean test

or

./gradlew clean build

Hope that works.

Community
  • 1
  • 1
nmfzone
  • 2,755
  • 1
  • 19
  • 32
  • 3
    Could you explain why "clean" would be needed? – Michael Kanis Jul 04 '17 at 07:52
  • 2
    @MichaelKanis because you've been changed the gradle configuration..as far as I know, the gradle configuration is cached. – nmfzone Jul 04 '17 at 20:21
  • 1
    @nmfzone that means you need to run clean test once to update the cache. But in my scenario, I need to run clean command every time just to show the test result on console. Even though no change to gradle config is made on following runs. – Weishi Z Jul 30 '17 at 04:22
  • 1
    @WeishiZeng are you sure that tests simply are not running because their inputs have not changed? If you have some test input other than java source files, you should register them as Task.inputs, so gradle can know when you need to rerun tests or not. – Ajax May 02 '18 at 14:21
16

Just to add, the:

showStandardStreams = true

is a shorthand for:

events = ["standard_out", "standard_error"]

It is important to keep this in mind when mixing both entries as the following:

test {
    testLogging {
        showStandardStreams = true
        events = ["passed", "failed", "skipped"]
    }
}

will result in no stdout whereas the reverse order:

test {
    testLogging {
        events = ["passed", "failed", "skipped"]
        showStandardStreams = true
    }
}

will add the stdout entries to the list, so stdout will work.

See the source for details.

Johnny Baloney
  • 3,409
  • 1
  • 33
  • 37
  • 1
    When `showStandardStreams` comes second in the ordering, just to double check, do the standard streams get _appended_ to the events list? It seems like it, but just double checking. – George Pantazes Jan 30 '19 at 16:04
14
test {
    testLogging.showStandardStreams = true
}

and

test {
    testLogging {
        showStandardStreams = true
    }
}

also works.

zhouji
  • 5,056
  • 1
  • 26
  • 26
13

If you are using Kotlin DSL with build.gradle.kts the syntax is a bit different.

Make sure you have the junit in your dependencies:

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter:5.4.2")
    testImplementation("org.junit.jupiter:junit-jupiter-api")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

Then you need to add to your test task:

import org.gradle.api.tasks.testing.logging.TestExceptionFormat

tasks.test {
    useJUnitPlatform()
    testLogging {
        showStandardStreams = true
        exceptionFormat = TestExceptionFormat.FULL
        events("skipped", "failed")
    }
}

Then you can adjust the settings based on your need.

Sylhare
  • 5,907
  • 8
  • 64
  • 80
9

For Android Gradle: https://stackoverflow.com/a/42425815/413127

For Android Gradle KTS (Kotlin):

// Test Logging
tasks.withType<Test> {
    testLogging {
        events("standardOut", "started", "passed", "skipped", "failed")
    }
}
Blundell
  • 75,855
  • 30
  • 208
  • 233
3
./gradlew --info clean build test

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
0

In my case I was working with Java and Spring-boot-starter-test.

I had the same issue and the problem was that I did not have any test engine.

So I add one to the dependencies of the build.gradle and it has work.

testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: dependencyVersion.junit5 testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: dependencyVersion.junit5

Iker Aguayo
  • 3,980
  • 4
  • 37
  • 49
0

Extending on @joshuakcockrell and @Blundell solutions (here and here), when dealing with a multi-module android app the following can be used (root build.gradle)

// Call from root build.gradle
setupTestLogging()

fun Project.setupTestLogging() {
    for (sub in subprojects) {
        sub.tasks.withType<Test> {
            testLogging {
                exceptionFormat = TestExceptionFormat.FULL
            }
        }
    }
}

(note that while exceptionFormat alone should be enough to get the wanted outcome, the events("standardOut" ...) mentioned above can be specified in the same way)

Marino
  • 800
  • 1
  • 12
  • 26