Testing
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
We are going to use Java plugin in this example to demonstrate how to use Gradle for testing our code.
First we apply Java plugin and add dependency to JUnit.
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
Then we can run all the tests: gradle test
Or we can run a single test using test property:
gradle :sub-module:test -Dtest=com.test.MyTest
We might have multiple types of tests in our projects. Typically, it is unit, integration and functional tests. Putting all the files into test folder is the default strategy, that could be considered as messy. We might want to put our tests into separate folders and run each set of tests separately. Unit tests should not require any special configuration. But the other tests, like integration, might require to start up databases or services.
Lets create unit and integration tests in the next Gradle build script.
// set system properties for tests
test {
// exclude integration tests
exclude '**/*IntegrationTest.class'
forkEvery = 5 // batch of 5, each processes will do 5 tests
maxParallelForks = (int) Runtime.runtime.availableProcessors() / 2
systemProperty 'hiThere', 'Hi :)'
testLogging {
showStandardStreams = true // turns on logging of standard and error output
exceptionFormat 'full'
events 'started', 'passed', 'skipped', 'failed' // to get info how much tests failed etc...
}
}
apply plugin: 'announce'
By default, all standard outputs are hidden. We can change it and see the printed output in the console while tests are running.
test {
testLogging {
exceptionFormat = 'full'
showStandardStreams = true
events "started", "passed", "skipped", "failed", "standardOut", "standardError"
}
}
See more in Gradle DSL documentation.
test {
reports {
junitXml.enabled = true
junitXml.outputPerTestCase = false
html.enabled = false
}
}