emisilva emisilva - 3 years ago 213
Java Question

Gradle test: show standard streams only with specific tests

While testing with Gradle, I'd like to show standard streams on the console only when I run a specific subset of tests with the

--tests
option.

Let me elaborate. On
build.gradle
I have

test {
testLogging {
//showStandardStreams = true
}
}


Usually, when I run my whole test suite, I don't like to have console output from the tests. But when I'm working on a specific test, it's very useful to have console output, so I go ahead and uncomment that line before executing that test with
gradle test --tests *name
.

I'd like to know if there's an automated way to do that.

EDIT: Let my clarify. I'd like to have console output when I run
gradle test --tests *name
, but not when I run
gradle test
.

Answer Source

To get console output on some runs for all tests, you could supply an additional parameter (-Poutput) on command-line:

gradle test --tests *name -Poutput

test {
    onOutput { descriptor, event ->
        if (project.hasProperty('output')) {
            logger.lifecycle(event.message)
        }
    }
}

To get console output on a specific test only:

test {
    onOutput { descriptor, event ->
        if (descriptor.name=='YOUR_TEST_METHOD_NAME' &&
                descriptor.className=='YOUR_PACKAGE_QUALIFIED_CLASS_NAME') {
            logger.lifecycle(event.message)
        }
    }
}

See TestDescriptor and Test

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download