TestNG XML file allows us to configure a test suite and execute it from the command line or ant script.
When we execute a TestNG test through Eclipse or through maven build, HTML reports are generated. These HTML reports also create a test suite XML file that we can use to execute the same test through the command line. If you are new to TestNG, please go through TestNG Tutorial.
TestNG XML
TestNG XML suite are based on following DTD: https://testng.org/testng-1.0.dtd
Before we look into some TestNG XML examples, let’s create a simple class and corresponding TestNG test class.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.journaldev.xml; public class Utils { public static String NAME = ""; public int add(int x, int y) { return x + y; } public static void setName(String s) { System.out.println("Setting NAME to " + s); NAME = s; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.journaldev.xml; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Optional; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class TestNGXMLTest { @Test(dataProvider = "dp", priority=1) public void test_add(Integer x, Integer y) { Utils u = new Utils(); Assert.assertEquals(u.add(x, y), x + y); } @Test(dataProvider = "dpName", priority=3, groups="setName") public void test_setName(String s) { Utils.setName(s); Assert.assertEquals(Utils.NAME, s); } @Test @Parameters("name") public void test_name(@Optional("NA") String s) { System.out.println("Input parameter = "+s); } @DataProvider public Object[][] dp() { return new Object[][] { new Object[] { 1, 1 }, new Object[] { 2, 2 }, }; } @DataProvider public Object[][] dpName() { return new Object[][] { new Object[] { "Utils" }, new Object[] { "MyUtils" }, }; } } |
TestNG XML Example
Let’s create a very simple TestNG XML test suite file.
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="TestNGXMLTest Test Suite" guice-stage="DEVELOPMENT" parallel="classes"> <test thread-count="5" name="TestNGXMLTest Test" verbose="2"> <classes> <class name="com.journaldev.xml.TestNGXMLTest" /> </classes> </test> </suite> |
Above TestNG XML file is very easy to understand. We have created a test suite and defined the class to execute.
parallel
attribute tells TestNG to run the test suite classes in parallel, thread-count
is used to define the number of maximum threads to use when running tests in parallel.
verbose
is used to define the amount of logging to be performed on the console.
When we execute above test suite XML file, we get following output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[RemoteTestNG] detected TestNG version 6.14.3 Input parameter = NA Setting NAME to Utils Setting NAME to MyUtils PASSED: test_name("NA") PASSED: test_add(1, 1) PASSED: test_add(2, 2) PASSED: test_setName("Utils") PASSED: test_setName("MyUtils") =============================================== TestNGXMLTest Test Tests run: 5, Failures: 0, Skips: 0 =============================================== =============================================== TestNGXMLTest Test Suite Total tests run: 5, Failures: 0, Skips: 0 =============================================== |
Below image shows how to run above TestNG XML suite file through command line.
We can also run it directly using Eclipse TestNG plugin.
TestNG XML Suite Packages
We can also define the packages to look for TestNG test classes. It’s handy when we have many test classes to execute.
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="TestNG Packages Test Suite" guice-stage="DEVELOPMENT"> <test thread-count="5" name="TestNG Packages Test" verbose="2"> <packages> <package name="com.journaldev.xml"></package> </packages> </test> </suite> |
Since I have only single TestNG test class in the package, the output will be same as earlier test.
TestNG XML Groups
We can specify which groups to include in test and which ones to exclude in XML test suite file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="TestNG Groups Test Suite" guice-stage="DEVELOPMENT"> <test thread-count="5" name="TestNG Groups Test" verbose="2"> <groups> <run> <include name="setName"></include> </run> </groups> <classes> <class name="com.journaldev.xml.TestNGXMLTest"> </class> </classes> </test> </suite> |
Above test suite will only execute methods in setName
group, so only test_setName
method will be executed.
TestNG XML Include Exclude Methods
By default, all the test methods are included from the test class. We can exclude some methods from the test suite using exclude
element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="TestNG Excludes Test Suite" guice-stage="DEVELOPMENT"> <test thread-count="5" name="TestNG Excludes Test" verbose="2"> <classes> <class name="com.journaldev.xml.TestNGXMLTest"> <methods> <exclude name="test_add"></exclude> </methods> </class> </classes> </test> </suite> |
TestNG XML Parameters
We can pass parameters to Test classes methods using parameter
element.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="TestNG Parameters suite" guice-stage="DEVELOPMENT"> <test thread-count="5" name="TestNG Packages Test" verbose="2"> <parameter name="name" value="JournalDev"></parameter> <!-- Test Level Parameters --> <classes> <class name="com.journaldev.xml.TestNGXMLTest" /> </classes> </test> </suite> |
When we run above test, the “name” parameter will be mapped to the test_name
method argument. You will notice following statements in the output console logs.
1 2 3 4 |
Input parameter = JournalDev PASSED: test_name("JournalDev") |
Read more at TestNG Parameters.
TestNG XML Listeners
We can also specify listeners for our TestNG XML test suite.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="TestNG Listeners Test Suite" guice-stage="DEVELOPMENT"> <listeners> <listener class-name="com.journaldev.listeners.Test3SuiteListener" /> <listener class-name="com.journaldev.listeners.Test3TestListener" /> </listeners> <test thread-count="5" name="TestNG Listeners Test" verbose="2"> <classes> <class name="com.journaldev.xml.TestNGXMLTest" /> </classes> </test> </suite> |
TestNG listeners is a great feature to monitor and modify test cases behavior, read more at TestNG Listeners.