Logging while testing through Gradle

JavaScalaLoggingGradle

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

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

Which is good to see more from stacktrace

Solution 3 - Java

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

Solution 4 - Java

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.

Solution 5 - Java

test {
    testLogging.showStandardStreams = true
}

and

test {
    testLogging {
        showStandardStreams = true
    }
}

also works.

Solution 6 - Java

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.

Solution 7 - Java

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.

Solution 8 - Java

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

Solution 9 - Java

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

Solution 10 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmissingfaktorView Question on Stackoverflow
Solution 1 - JavarobyView Answer on Stackoverflow
Solution 2 - JavaTo KraView Answer on Stackoverflow
Solution 3 - JavajoshuakcockrellView Answer on Stackoverflow
Solution 4 - JavanmfzoneView Answer on Stackoverflow
Solution 5 - JavazhoujiView Answer on Stackoverflow
Solution 6 - JavaJohnny BaloneyView Answer on Stackoverflow
Solution 7 - JavaSylhareView Answer on Stackoverflow
Solution 8 - JavaGayan WeerakuttiView Answer on Stackoverflow
Solution 9 - JavaBlundellView Answer on Stackoverflow
Solution 10 - JavaIker AguayoView Answer on Stackoverflow