Testing with Gradle Java Plugin: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 105: Line 105:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
./gradlew test --debug-jvm ...
./gradlew test --debug-jvm ...
</syntaxhighlight lang=''>
</syntaxhighlight>
For more details, see: {{Internal|Debugging Gradle Tests|Debugging Gradle Tests}}
For more details, see: {{Internal|Debugging Gradle Tests|Debugging Gradle Tests}}
==Running Individual Tests==
==Running Individual Tests==
Line 115: Line 115:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
./gradlew :my-subproject:test --tests playground.my.package.MyTest.myTestMethod
./gradlew :my-subproject:test --tests playground.my.package.MyTest.myTestMethod
</syntaxhighlight lang=''>
</syntaxhighlight>
Multiple tests:
Multiple tests:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
Line 126: Line 126:
gradle someTestTask --tests *UiTest someOtherTestTask --tests *WebTest*ui
gradle someTestTask --tests *UiTest someOtherTestTask --tests *WebTest*ui
</syntaxhighlight>
</syntaxhighlight>
 
==Fail Fast==
 
Exit after the first test failure
 
<syntaxhighlight lang='bash'>
 
./gradlew test --fail-fast ...
 
</syntaxhighlight>


=Testing Logging=
=Testing Logging=

Latest revision as of 20:31, 10 June 2021

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.

gradlew test [--debug-jvm] [--fail-fast]  [--tests <class-or-method-name-*-is-supported>]

JUnit and TestNG are both supported.

Gradle executes tests in a separate ("forked") JVM. None of the Gradle system properties are passed by default to the forked JVM. To pass system properties of interest, the plugin as to be configured as described below.

Because the tests are executed in a forked JVM, they are isolated from the main build process and the main build process is isolated 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}"
   ...
}

Interesting properties:

Gradle Project Properties

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

Default is 1.

test {
  maxParallelForks = 1
}

A tests can distinguish between parallel test JVMs by using the value of the org.gradle.test.worker property, which is unique for each process. It can be used for anything that's needed, but it’s particularly useful for filenames and other resource identifiers to prevent concurrent access conflicts.

test Task Options

Debugging a Gradle Test

./gradlew test --debug-jvm ...

For more details, see:

Debugging Gradle Tests

Running Individual Tests

To run all tests from a class:

./gradlew :my-subproject:test --tests playground.my.package.MyTest

To run only one individual test method:

./gradlew :my-subproject:test --tests playground.my.package.MyTest.myTestMethod

Multiple tests:

gradle test --tests *SomeTest.someSpecificFeature
gradle test --tests *SomeSpecificTest
gradle test --tests all.in.specific.package*
gradle test --tests *IntegTest
gradle test --tests *IntegTest*ui*
gradle test --tests *IntegTest.singleMethod
gradle someTestTask --tests *UiTest someOtherTestTask --tests *WebTest*ui

Fail Fast

Exit after the first test failure

./gradlew test --fail-fast ...

Testing Logging

TODO

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