TestNG XML

Filed Under: TestNG

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: http://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.


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;
	}
}

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.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://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.


[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.

testng xml example

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.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://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.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://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.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://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.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://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.


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.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://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.

You can download TestNG XML suite code from our GitHub Repository.

Comments

  1. Purvi says:

    It’s a good article. Thank you so much. i am developing a hybrid framework of an application where around 10 modules and when we mouse hover on it it gives 5-6 options/link/new page after clicking each of it i have to a write a script for it. Say example User Management module having -4 option EditProfile page, Chang Password Page, AboutPage and LogOut page. Same way each of module have same way options/link or page. How do i create testNg.xml file. Should i create individual package for each module? and execute each module/package . Just give me an idea how should i create my testng.xml file. Please reply will be a great help to me.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages