In any project, you will end up to a place where you need to execute so many test cases on a run. Running a set of test cases together is call executing a Test Suite. Those test cases can be dependent to each other or may have to be executed in a certain order. TestNg gives us the capability to manage our test execution.
TestNG Test Suite
In the TestNG framework, we need to create testng.xml file to create and handle multiple test classes. This is the XML file where you will configure your test run, set test dependency, include or exclude any test, method, class or package and set priority, etc.
How to do it…
Step 1 : Create a TestNG XML
1) Right-click on the Project folder, go to New and select ‘File‘ as shown in the below image.
2) In New file wizard, add file name = ‘testng.xml‘ as shown in the below-given image and click on the Finish button.
3) It will add testng.xml file under your project folder.
Step 2 : Write XML code?
1) Now add below-given code in your testng.xml file.
<suite name="Any Name" >
<test name="Any Name" >
<classes> <class name="PackageName.TestCaseName" />
Note: You can choose any name for your Test Suite & Test Name as per your need.
2) After giving appropriate names, now your testng.xml file will looks like this:
Hope you have understood the XML code, as it is quite simple hierarchy:
Very first tag is the Suite tag<suite>, under that it is the Test tag<test> and then the Class tag<classes>. You can give any name to the suite and the test but you need to provide the correct name to the <classes> tag which is a combination of your Package name and Test Case name.
Step 3 : Execute a testng.xml
Now it’s time to run the XML. Run the test by right click on the testng.xml file and select Run As > TestNG Suite.
It will take a few seconds to start the testng execution engine and soon you will notice that your test will run and complete. Once the execution is complete, you can view test execution results under the TestNg console.
This is the one simple example of creating and running testng.xml file in eclipse.
Building a Test Suite
Now when you have learned how to build the XML, now it’s time to learn how to build a Test Suite using testng.xml. It is again not a complex task, all you need to do is to add your test cases to your XML file in <classes> tag.
The above test will execute only those tests, which are mentioned in the testng.xml. The rest of the test cases under ‘automationFramework’ package will remain untouched.