Creating Test Suites Using TestNG XML
Guide to creating a basic test suite in TestNG XML
To create a test suite using testng.xml file, we first write the <suite></suite> tag. This is the root element of the test suite document and consists of all the tests that we want to run. The ‘suite’ element contains a name parameter that tells what the name of the test suite is.
In the example above, this feature is implemented as:
<suite name=”Regression Suite”>
<test></test>
.
.
.
<test ></test>
<suite />
Inside the suite tag, we can have more than one test tags, each for a set of test cases to be executed. Building up from the previous example as:
<suite name=”Regression Suite”>
<test name=”Functionality Test”></test>
<test name=”UI Test”></test>
<suite />
Please use readable and appropriate names for the suite tag and the test tags to improve code readability.
Including and excluding test classes and methods in the suite
In the example testng.xml we have taken above, the test tag named functionality test we have the classes tag. Each test tag can have only one classes tag. The classes we have included in this test are LoginTest and SearchTest in the Regression Test and ScrollTest and NightModeTest for the UI Testing. This is the following part in the code above:
<classes>
<class name=”com.example.tests.ScrollTest”/>
<class name=”com.example.tests.NightModeTest” />
</classes>
Within the class tag, we have an optional methods tag. If we choose to omit it, then by default all the tests in class are run. Otherwise, we can choose to include or exclude the methods as we want.
This can be seen from the following example:
<class name=”com.example.tests.SearchTest”>
<methods>
<include name=”searchByProduct”/>
<exclude name=”searchByCategory”/>
</methods>
</ class>
We have a class called Search Test and inside that class, we choose to include the method searchByProduct in execution and exclude the method searchByCategory from execution.
Parameterizing test suite configuration
Test parameterization in test suite is the key to test methods with different datasets. To learn deeply about parameterizing test suite configuration, please visit this page.
So, now let us define the contents of our testng.xml file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Regression Test Suite">
<test name="Search Test">
<classes>
<class name="com.w3wiki.browsertests.TestClassA">
<methods>
<include name="testWebPageSearch" />
</methods>
</class>
<class name="com.w3wiki.browsertests.TestClassB" >
<methods>
<include name="testNavBarSearch"/>
</methods>
</class>
</classes>
</test>
</suite>
Our project is complete. We will now look at ways to run it.
How to Create Test Suite in TestNG?
In this article we will learn about test suites and how can we build them using Java and TestNG. TestNG is a modern testing framework that is used very widely used today. It has a broad base of features that allow us to write unit tests and club them together in groups.
Table of Content
- What is a Test Suite?
- Steps to Create Test Suite in TestNG
- Understanding TestNG XML Configuration
- Creating Test Suites Using TestNG XML
- Running Test Suites
- Conclusion
- FAQ’s on How to Create Test Suite in TestNG
TestNG is used in combination with Selenium to implement browser automation and therefore used for regression testing. By the end of this article, you will be successfully creating and running your own test suites.