How to run TestNG xml file using gradle
Posted on
Gradle is a powerful build automation tool that can be used to run TestNG tests easily. In this blog, I'll explain how to configure and run a TestNG XML file using Gradle, step by step.
Step 1. Create a Gradle Project
If you don't have a Gradle project, create one using the following command:
gradle init
You can also use an existing Gradle project.
Step 2. Add TestNG Dependency in build.gradle
You need to add the TestNG library to your Gradle project.
Modify your build.gradle file:
plugins { id 'java' } repositories { mavenCentral() } dependencies { testImplementation 'org.testng:testng:7.5' } test { useTestNG() // Tells Gradle to use TestNG }
Step 3. Create a testng.xml File
Place this file inside the src/test/resources folder.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="TestSuite"> <test name="SampleTest"> <classes> <class name="com.example.tests.SampleTest"/> </classes> </test> </suite>
Step 4. Create a TestNG Test Class
package com.example.tests; import org.testng.annotations.Test; public class SampleTest { @Test public void testMethod() { System.out.println("TestNG test is running..."); } }
Step 5. Modify build.gradle to Use testng.xml
By default, Gradle runs all test classes. To run a specific TestNG XML file, update build.gradle like this:
test { useTestNG { suites 'src/test/resources/testng.xml' // Specify the testng.xml file } }
Step 6. Run TestNG Tests Using Gradle
Now, open a terminal and run the following command:
gradle test
Expected Output:
Gradle will execute the tests as defined in testng.xml, and you will see output like:
> Task :test
TestNG test is running...
BUILD SUCCESSFUL