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):
Basic Structure of testng.xml
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
If you want to include all classes under a specific package, you can use the
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
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