Executing test cases using testng xml file
One of the important features of TestNG is the execution of test cases from an XML file (testng xml file)
Testng.xml file allows us to execute test cases from a single class, multiple classes, all the classes of a package, multiple packages, execution of groups of test cases and much more.
Let us consider some example why we need these capabilities:
Example 1: we have 5 features in an application under test and for each feature, we have created one java class and you want to execute all the test cases of only 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:
There are two ways to do this:
In eclipse, right click the TestNG class which you want to convert as testng xml file –> TestNG –> convert to TestNG. Rename the suite, rename the test and save the file.
Follow below steps to create testng.xml file for the first time.
- Create a Testng class with say one dummy test case.
- Execute the test case as testng (Right Click class name –> Run As –> TestNG Test)
- This will create a test-output folder in your project.
- In the test-output folder, there is an index.html file. Open index.html file (Right click –> Open with –> Web Browser)
- A Test report will open up. In the Info section, there is a link for testng-customsuite.xml. Click it.
- 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 ).
- 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 by passing an attribute tests=parallel in suite tag.
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.
To execute a testng xml file, right click testng.xml file –> Run as TestNG suite.
In upcoming tutorials of TestNG, we will learn what we all different scenarios we can execute using TestNG and Testng.xml file which we discussed above.
For any questions, queries, feedback and suggestions. Feel free to drop a mail at firstname.lastname@example.org or email@example.com.