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:

XML
<!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.

Similar Reads

What is a Test Suite?

A test suite is a collection of test cases. There are various ways to create test these. You could either choose to group these tests by functionality or you could simply club your test classes together to form a test suite. In TestNG, the test suite is written in the form of a xml document. This allows you as a tester to have a fine grained control over what tests you want to run and create groups or suites according to the very specific requirements of your project....

Steps to Create Test Suite in TestNG

Step 1. Setting Up TestNG Environment...

Understanding TestNG XML Configuration

Introduction to TestNG XML configuration file...

Creating Test Suites Using TestNG XML

Guide to creating a basic test suite in TestNG XML...

Running Test Suites

Running Test Suites From IDE...

Conclusion

In this article we looked at what test suites are and how we can create them using TestNG in java. We looked at two approaches to clubbing tests, through groups and through classes. Hope you learnt something useful. If you want to discuss something, please feel free to leave comments below....

FAQ’s on How to Create Test Suite in TestNG

Where can I use the tag inside testng.xml?...