Testing with Gradle Java Plugin

From NovaOrdis Knowledge Base
Revision as of 22:39, 14 January 2019 by Ovidiu (talk | contribs) (→‎Overview)
Jump to navigation Jump to search

External

Internal

Overview

Testing is performed by the "test" task (implemented by Test), which automatically detects and executes all unit tests in the test source set and generates a report once test execution is complete. JUnit and TestNG are both supported.

Gradle executes tests in a separate ("forked") JVM, thus isolating tests from the main build process and isolating the main build process from tests. This prevents excessive memory consumption by the main build process. It also allows running the tests with different JVM arguments. By default, there's just one forked JVM. More than one can be configured with maxParallelForks.

Configuring the "test" Task

Use a script block in build.gradle:

test {
   useJUnit()
   maxHeapSize = '1G'
}

Passing System Properties to Tests

test {
   ...
   systemProperty 'rootDir', "${rootDir}"
   ...
}

Also see Maven Surefire Configuration Example.

Show stdout and stderr of the Test JVM on the Console

test {

   testLogging.showStandardStreams = true
}

Test Execution Mechanics Logging Output

A quick way to generate Gradle test execution code logging is to start the test task with --debug:

gradle test --debug

Running gradle test in this mode could prove helpful to troubleshooting test logging configuration issues such as:

14:18:50.105 [DEBUG] [TestEventLogger]
14:18:50.105 [DEBUG] [TestEventLogger] Gradle Test Executor 1 STARTED
14:18:50.111 [DEBUG] [TestEventLogger]
14:18:50.111 [DEBUG] [TestEventLogger] com.example.SomeTests STARTED
14:18:50.186 [DEBUG] [TestEventLogger]
14:18:50.186 [DEBUG] [TestEventLogger] com.example.SomeTests STANDARD_ERROR
14:18:50.186 [DEBUG] [TestEventLogger]     SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
14:18:50.188 [DEBUG] [TestEventLogger]     SLF4J: Defaulting to no-operation (NOP) logger implementation
14:18:50.188 [DEBUG] [TestEventLogger]     SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
14:18:50.948 [DEBUG] [TestEventLogger]

The "testLogging" property represents a set of options that control which test events are logged and at what level.

test {
    testLogging {
      ...
    }
}

For more details see TestLoggingContainer.

Number of Forked JVMs

test {
    testLogging {
      ...
    }
}

TODO

TODO process https://docs.gradle.org/current/userguide/java_testing.html