Understanding TestNG XML Configuration

Introduction to TestNG XML configuration file

The testNG XML configuration is defined in the file called as testng.xml in the root directory of your project. It defines how your tests should be run and allows you to have fine-grained control of the unit testing processes. The name should not be modified otherwise you will not be able to run your tests. Instead of running your tests one class at a time, this file allows you to run all your tests inside all your test classes at once.

Structure of TestNG XML file (test suite, test, classes, methods)

The elements that are added to the file in the form of xml tags have a pre-defined hierarchy that should not be altered. The main elements we use in this file are: suite, test, classes, include and exclude tags.

A typical structure of testng.xml file is as follows:

XML
<suite name="RegressionSuite">
  <test name="Functionality Test">
    <classes>
      <class name="com.example.tests.LoginTest"/>
      <class name="com.example.tests.SearchTest">
      <methods>
          <include name="searchByProduct"/>
          <exclude name="searchByCategory"/>
        </methods>
      </ class>
    </classes>
  </test>
  <test name="UI Test">
    <classes>
      <class name="com.example.tests.ScrollTest"/>
      <class name="com.example.tests.NightModeTest" />
    </classes>
  </test>
</suite>

Defining test suite, test, and test classes in XML file

  • Test Suite: This is the root level tag of the xml tree. Each file can have only one of these tags.
  • Test Tag: This tag is defined inside the suite tag and there can be many test tags inside the suite tags. For example: <test name=”UI Test”></test>, <test name=”Functionality Test”></test> etc.
  • Test Classes: This tag is inside of the test tag. Again, you can have only one of the <classes /> tag defining the classes you want to have in your test. These classes must be relevant to the name of the parent test tag. Please refer to the xml code above for better understanding.

We will understand these concepts in more detail in the next section.

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