Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 970
e18r Avatar asked May 17 '15 00:05

e18r


1 Answers

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

like image 96
tnabeel Avatar answered Oct 24 '22 03:10

tnabeel