TestNG XML With Examples

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.

TestNG XML Example

Let’s create a very simple TestNG XML test suite file.

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.

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.

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.

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.

TestNG XML Parameters

We can pass parameters to Test classes methods using parameter element.

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.

Read more at TestNG Parameters.

TestNG XML Listeners

We can also specify listeners for our TestNG XML test suite.

TestNG listeners is a great feature to monitor and modify test cases behavior, read more at TestNG Listeners.

By admin

Leave a Reply

%d bloggers like this: