TestNG - Gradle example

Gradle is an open-source build automation tool that is designed to be flexible enough to build almost any type of software. Gradle runs on the JVM and you must have a Java Development Kit (JDK) installed to use it. You can find more information on Gradle

You can install the Gradle build tool on Linux, macOS, or Windows.

Gradle runs on all major operating systems and requires only a Java Development Kit version 8 or higher to run. To check, run java -version. Please check gradle installation documentation

Once you have installed Gradle, here are some of the useful commands or examples :-

How to add dependencies to build.gradle?

Adding a dependency into your gradle project is similar to the way we do in Maven. From the maven repository, you have a gradle tab, Just click Gradle tab, You’ll find your dependency which needs you to add to your build.gradle file in a parent folder.

Add Gradle dependency

By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. When we specify useTestNG(), Gradle scans for all the methods annotated with @Test and execute them.

Example 1. Below is the basic configuration for the 'test' task

plugins {
    id 'java'
}

group 'com.test'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.testng', name: 'testng', version: '6.14.3'
}

test {
    useTestNG()
}

How to Include TestNG Groups in build.gradle ?

In our automation, we do use TestNG groups to groups functionalities/features and also like 'regression', 'smoke', 'sanity' etc.

Using build.gradle file, we can configure which test groups to include or exclude during the test execution via useTestNG() setting, as seen below:

Grouping TestNG tests, to execute 'smoke' and 'sanity' tests

test {
     useTestNG() {
        includeGroups 'smoke', 'sanity'
        excludeGroups 'regression'
    }
}

// To show standard out and standard error of the test JVM(s) on the console

testLogging.showStandardStreams = true

How to generate TestNG Report in Gradle ?

If you really want to use all default TestNG reports, we need to set useDefaultListeners property. By default, Gradle sets it to 'false' so that Gradle reports are generated.

When we set this property to 'true', the default listeners are a set of reporters that generate TestNG HTML report, XML results in JUnit format, emailable HTML test report and XML results in TestNG format.

useTestNG() {
    // To generate reports by TestNG library
    useDefaultListeners = true
     }

By default, the HTML report will be stored in the test task's. We can set the testng-output report directory by using outputDirectory. This allows testng to write TestNG's output in the specified location.

We also need to specifically set Gradle's HTML report to 'false' to avoid replacing testng html report.

test {
    useTestNG() {
        useDefaultListeners = true
        //set TestNG output dir
       // outputDirectory = file("$projectDir/testngOutput")
    }

    // turn off Gradle's HTML report to avoid replacing the
    // reports generated by TestNG library
    reports.html.enabled = false
}

NOTE : - If you also need Gradle's HTML report along with testng report, we can specify the directory location like below.

The default location for gradle html report $buildDir/reports/tests/test

 reports.html.setDestination(file("$buildDir/html"))

If you want to define report directory using command line, we can do like below : -

useTestNG {
    def dir = System.getProperty("testngReportOutput") 
    outputDirectory = dir ? file(dir) : file("$buildDir/testng-output")
    }

Using -D command-line option, we can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command.

gradle -DtestngReportOutput=testReport clean test

The above command will have testng report in the root project directory.

How to use testng listeners in gradle ?

If you have to use any listeners to make advanced or customized testng reports, we can add them to build.gradle file. Add fully qualified listener classes to build.gradle file like below :

 test {
     useTestNG() {
       // creates emailable HTML file
       // this reporter typically ships with TestNG library
      listeners << 'com.listeners.TestListener'
     }
 }

How to execute single test method or class from Gradle ?

We can use gradle test filter option to make this possible. This allows filtering tests for execution using --tests.

Using --tests option, we can also limit testing to a single method within the test class. This helps you to run a single method/class at a time.

Example to run single class :-

gradle test --tests com.example.testClass

The above command will run your single class 'testClass'.

Example to run a single test method :-

gradle test --tests com.example.testClass.testMethod

The above command will run single method 'testMethod' within your 'testClass'. And if you want to execute multiple test methods using a wildcard like below :-

gradle test --tests com.example.testClass.*someMethod*

Example to execute specific test class(s) using wildcard :-

gradle test --tests com.example.testClass*

The above command will execute classes that starts with file name 'testClass' like 'testClassOne' / 'testClassTwo' etc.

To display the test result in the console, we can add the following test events in build.gradle file like below :-

test {

    useTestNG() {
        // report generation delegated to TestNG library:
        useDefaultListeners = true
    }
    //we want display the following test events
    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
    }
}

After running gradle test, It runs the test classes, methods and status will be displayed beside.

Gradle test result status

Test Frameworks: 

Comments

Could you please advise, what path should be while adding a custom listener?

I have created a listener, which has the following path "exampleproject/ui-test/src/main/java/utils/MyTestListerAdapter.java"

I have added it to the build.gradle, but Gradle could not find a file:

test {
useTestNG(){
suites 'src/test/resources/testng.xml'

options {
listeners.add("src/main/java/utils/MyTestListerAdapter.java")
}

}

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.