Gradle - Testing


Advertisements


The test task automatically detects and executes all unit tests in the test source set. It also generates a report once test execution is complete. JUnit and TestNG are the supported APIs.

The test task provides a Test.getDebug() method that can be set to launch to make the JVM wait for a debugger. Before proceeding to the execution, it sets the debugger post to 5005.

Test Detection

The Test Task detects which classes are test classes by inspecting the compiled test classes. By default it scans all .class files. You can set custom includes / excludes, only those classes will be scanned. Depending on the test framework used (JUnit / TestNG), the test class detection uses different criteria.

When using JUnit, we scan for both JUnit 3 and 4 test classes. If any of the following criteria match, the class is considered to be a JUnit test class −

  • Class or a super class extends TestCase or GroovyTestCase
  • Class or a super class is annotated with @RunWith
  • Class or a super class contain a method annotated with @Test
  • When using TestNG, we scan for methods annotated with @Test

Note − The abstract classes are not executed. Gradle also scans the inheritance tree into jar files on the test classpath.

If you don't want to use test class detection, you can disable it by setting scanForTestClasses to false.

Test Grouping

JUnit and TestNG allows sophisticated groupings of test methods. For grouping, JUnit test classes and methods JUnit 4.8 introduces the concept of categories. The test task allows the specification of the JUnit categories you want to include and exclude.

You can use the following code snippet in build.gradle file to group test methods.

test {
   useJUnit {
      includeCategories 'org.gradle.junit.CategoryA'
      excludeCategories 'org.gradle.junit.CategoryB'
   }
}

Include and Exclude Particular Tests

The Test class has an include and exclude method. These methods can be used to specify, which tests should actually be run.

Run only the included tests −

test {
   include '**my.package.name/*'
}

Skip excluded tests −

test {
   exclude '**my.package.name/*'
}

The sample build.gradle file as shown below it shows different configuration options.

apply plugin: 'java' // adds 'test' task

test {
   // enable TestNG support (default is JUnit)
   useTestNG()

   // set a system property for the test JVM(s)
   systemProperty 'some.prop', 'value'

   // explicitly include or exclude tests
   include 'org/foo/**'
   exclude 'org/boo/**'

   // show standard out and standard error of the test JVM(s) on the console
   testLogging.showStandardStreams = true

   // set heap size for the test JVM(s)
   minHeapSize = "128m"
   maxHeapSize = "512m"

   // set JVM arguments for the test JVM(s)
   jvmArgs '-XX:MaxPermSize=256m'
   
   // listen to events in the test execution lifecycle
   beforeTest { 
      descriptor → logger.lifecycle("Running test: " + descriptor)
   }

   // listen to standard out and standard error of the test JVM(s)
   onOutput { 
      descriptor, event → logger.lifecycle
         ("Test: " + descriptor + " produced standard out/err: " 
         + event.message )
   }
}

You can use the following command syntax to execute some test task.

gradle <someTestTask> --debug-jvm


Advertisements