Testing with Gradle Java Plugin: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(10 intermediate revisions by the same user not shown)
Line 10: Line 10:
=Overview=
=Overview=


Testing is performed by the "[[Gradle_Java_Plugin#tes|test]]" task (implemented by [https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html 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.
Testing is performed by the "[[Gradle_Java_Plugin#tes|test]]" task (implemented by [https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html Test]), which automatically detects and executes all unit tests in the test source set and generates a report once test execution is complete.  
 
<syntaxhighlight lang='bash'>
gradlew test [--debug-jvm] [--fail-fast]  [--tests <class-or-method-name-*-is-supported>]
</syntaxhighlight>
 
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 [[Testing_with_Gradle_Java_Plugin#Passing_System_Properties_to_Tests|described below]].
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 [[Testing_with_Gradle_Java_Plugin#Passing_System_Properties_to_Tests|described below]].
Line 37: Line 43:
</syntaxhighlight>
</syntaxhighlight>


Interesting properties: {{Internal|Gradle Project Properties|Gradle Project Properties}}
Interesting properties: {{Internal|Gradle Project Properties TODEPLETE|Gradle Project Properties}}


Also see [[Maven_Surefire_Plugin#Configuration_Example|Maven Surefire Configuration Example]].
Also see [[Maven_Surefire_Plugin#Configuration_Example|Maven Surefire Configuration Example]].
Line 95: Line 101:
A tests can distinguish between parallel test JVMs by using the value of the <tt>org.gradle.test.worker</tt> 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.
A tests can distinguish between parallel test JVMs by using the value of the <tt>org.gradle.test.worker</tt> 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.


=Testing Logging=
=<tt>test</tt> Task Options=
 
==Debugging a Gradle Test==
* [[Spring_Framework_Testing_Concepts#Test_Logging|Gradle-built Spring Framework Application Test Logging]]
* [[Gradle-built Generic Java Application Test Logging|Gradle-built Generic Java Application Test Logging]]
 
=Debugging a Gradle Test=
 
<font color=darkgray>Further research is needed:
 
Start Gradle in debug mode:
 
</font>
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
./gradlew --no-daemon -Dorg.gradle.debug=true :my-subproject:test
./gradlew test --debug-jvm ...
</syntaxhighlight>
</syntaxhighlight>
 
For more details, see: {{Internal|Debugging Gradle Tests|Debugging Gradle Tests}}
<font color=darkgray>It kept starting the daemon, in debug mode, and I could not debug the test. The default port is 5005.</font>
==Running Individual Tests==
 
To run all tests from a class:
TODO: https://docs.gradle.org/current/userguide/java_testing.html#sec:debugging_java_tests
 
=Running Individual Tests=
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
./gradlew :my-subproject:test --tests playground.my.package.MyTest
./gradlew :my-subproject:test --tests playground.my.package.MyTest
</syntaxhighlight>
</syntaxhighlight>
 
To run only one individual test method:
<syntaxhighlight lang='bash'>
./gradlew :my-subproject:test --tests playground.my.package.MyTest.myTestMethod
</syntaxhighlight>
Multiple tests:
Multiple tests:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
gradle test --tests *SomeTest.someSpecificFeature
gradle test --tests *SomeTest.someSpecificFeature
Line 131: 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=
* [[Spring_Framework_Testing_Concepts#Test_Logging|Gradle-built Spring Framework Application Test Logging]]
* [[Gradle-built Generic Java Application Test Logging|Gradle-built Generic Java Application Test Logging]]


=TODO=
=TODO=

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