How to Use TestNG with Gradle

Hello, I am a huge fan of Maven when it comes to automation project. But i recently joined in a team where Gradle is used companywide. What is Gradle? Gradle is an open-source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring the project configuration.

As far as I see Gradle does what Maven does when it comes to configuration and execution Here might be some advantages or disadvantages but I haven’t gotten into deeper parts of it. So I am going tell my story and some tips and tricks based on my experience.

Dependencies

Adding a dependency into your project is a straightforward process. Just go to https://mvnrepository.com/ and select Gradle tab. You’ll find your dependency. Add that to your build.gradle file in a parent folder.

There are two ways of scoping when it comes to adding a dependency to your project:

  • Compile: You’ll be able to use those kinds of libraries in your main and test folders.
  • testCompile: You’ll be able to use those kinds of libraries only in test folders.

How to Add TestNG XML into Gradle File

Open your build.gradle file and add those line at the end of it.

test { //this is the gradle task to be executed
  useTestNG() { //Tells Gradle to use TestNG
       useDefaultListeners = true // Tells TestNG to execute its default reporting structure
       suites 'src/test/suite.xml' //location of our suite.xml
  }
}

Running Specific Group of Tests via CLI

There are times we need to group our tests into logical groups like smoke, regression and execute them.

First, you need to group your tests by using TestNG’s default grouping feature. You can find an example in that link>http://www.swtestacademy.com/testng-tutorial/

Here’s a sample also:

@Test(groups="smoke")
public void purchaseWithMasterCard() {
}

@Test(groups="smoke")
public void purchaseWithVisaCard() {
}

Then we create a new Gradle task in build.gradle file as below.

task smokeTests(type: Test) { // we define a task with name ‘smokeTests’
   useTestNG() {
       useDefaultListeners = true
       suites 'src/test/suite.xml'
       includeGroups 'smoke' // We define the group name by using ‘includeGroups’ parameter.
   }
}

As you see, we don’t need to do any changes in our XML file.

Finally, we execute “gradle smokeTests” command via CLI and see the results like a charm. Now the only test with smoke groups will start.

2 thoughts on “How to Use TestNG with Gradle”

  1. what is the gradle command to run specific test from testng.xml file, for example in below testng.xml file I would like to run Regression test:

    Reply
  2. Finally, we execute “gradle smokeTests” command via CLI and see the results like a charm. Now the only test with smoke groups will start.

    how?

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.