TestNG XML (testng.xml)

testng.xml is a configuration file used by the TestNG framework to organize and control the execution of test cases. The TestNG XML configuration file must have a .xml extension and follow the structure defined by the TestNG Documentation. While it is commonly named testng.xml, any valid XML file name may be used, provided it adheres to the required format.

Use Cases of testng.xml

     To define and group test classes/packages
     To include/exclude specific methods or groups
     To pass parameters to tests
     To configure listeners, parallel execution, priorities, and suites
     To create flexible and maintainable test suites

TestNG XML File Declaration (DTD Reference)At the top of the XML file, always declare the DTD (Document Type Definition):

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">


Basic Structure of testng.xml

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1">
    <test name="Regression1">
        <classes>
            <class name="test.sample.ParameterSample" />
            <class name="test.sample.ParameterTest" />
        </classes>
    </test>
</suite>


Explanation: 

     Defines a suite named Suite1.
     Inside this suite, there is a test group named Regression1.
     It includes two specific test classes:
          test.sample.ParameterSample
          test.sample.ParameterTest

When executed, all @Test methods inside these two classes will run in the default order defined by TestNG.

Using Instead of

If you want to include all classes under a specific package, you can use the tag:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1">
    <test name="Regression1">
        <packages>
            <package name="test.sample" />
        </packages>
    </test>
</suite>


Explanation: 

     Executes all classes inside the test.sample package.
     Useful when you want to include an entire package without listing each class individually.

Including or Excluding Groups and Methods

You can selectively include or exclude specific groups using the tag:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1">
    <groups>
        <run>
            <exclude name="brokenTests" />
            <include name="checkinTests" />
        </run>
    </groups>
</suite>


Explanation:

     Includes only tests that belong to the checkinTests group.
     Excludes all tests marked with the brokenTests group.

This configuration will be helpful when you want to dynamically select or skip certain groups without modifying the code.

In next tutorial, we will see how to execute TestNG Script
 

Related Tutorials