TestNG xml

August 7, 2016

One of the important features of TestNG is the execution of Test cases from an XML file.

Testng.xml file allows us to execute test cases from multiple classes. Say for example we have 5 features in an application under test and for each feature, we have created one java class. Now say you want to execute all the test cases of two features at a time, this can be done using testng.xml. We can include multiple classes  in a test suite.

Testng.xml file also allows us to execute test cases in groups, say for example you want to execute Sanity Testcases of all the five features in above application under test. Sanity test cases consist of test cases from different classes. This can be done by creating groups in TestNG and then executing a particular group.

Creating Testng.xml file for the first time:

Follow below steps to create testng.xml file for the first time.

  1. Create a Testng class with say one dummy test case.
  2. Execute the test case as testng (Right Click class name –>  Run As –> TestNG Test)
  3. This will create a test-output folder in your project.
  4. In the test-output folder, there is an index.html file. Open index.html file (Right click –> Open with –> Web Browser)
  5. A Test report will open up. In the Info section, there is a link for testng-customsuite.xml. Click it.
  6. An xml will open up. Copy paste the xml in a new file with some name say testng.xml (In the package where you want to create the test suite, Right click –> New –> File ).
  7. Change the name of suite and test in the xml file.


Understanding Testng.xml file:

Testng.xml file starts with a Suite (Test Suite to be executed). A Test suite is represented by a <suite> tag.

A Test Suite can have multiple Tests. A Test is represented by a <test> tag.

By default, tests are executed sequentially, but can also be executed parallelly.

Classes (represented by <class> tag) to be executed are included under <classes> tag.

A Test can contain multiple classes.

Groups (represented by <groups> tag) to be included or excluded are also defined under <test> tag.


Above is an example of testng.xml which has a test suite with name “Feature_A” and a Test with a name “Testrun_A”.

We have included a class in it with name “TestingTestng” and we are executing all Sanity Test cases of this class.

In upcoming tutorials of TestNG, we will learn what we all different scenarios we can execute using TestNG and Testng.xml file.

For any questions, queries, feedback and suggestions. Feel free to drop a mail at saurabh@qatechhub.com or support@qatechhub.com.


About the Author


Follow Saurabh:

Leave a Comment: