Importance of XML file in TestNG Configuration
In TestNG, you can define multiple test cases in a single class whereas, in Java, you can define only one test in a single class in the main() method. In Java, if you want to create one more test, then you need to create another java file and define the test in the main() method.
Instead of creating test cases in different classes, we recommend you to use TestNG framework that allows you to create multiple test cases in a single class.
You can create multiple test cases with the help of @Test annotation.
Let's understand through an example.
The above code consists of a class test. The class test consists of two test cases, i.e., test1() and test2(). You can differentiate the test cases by considering the sequence of test cases. In the above code, the test case test2() is written in the second @Test annotation, so this test case will be considered as the second case.
Note: You can trigger all the test cases from a single file known as xml file. Xml file is the heart of TestNG framework.
How to create a xml file
In the above source code of xml file, suite is at the higher hierarchy in TestNG. Inside the
Now we will create the module of personal loan.
Step 1: We first create two java files and both the files contain test cases.
Step 2: Now we will create the xml file.
In the above XML file, we have created the suite "loan_department". We have created the module "Personal loan" inside the suite and within this module, we have created the test cases defined in the classes day1.module1 and day1.module2, where day1 is the package name and module1 and module2, are the classes.
Step 3: In this step, we will run the test cases. Now we do not need to run the java files individually. We have to run the XML file which will automatically execute all the test cases as we have configured all the class files inside the XML file that are containing test cases.
Right click on the testng.xml file and then move down to the Run As and then click on the1 TestNG Suite.
In the above output, we observe that all the test cases run successfully without any failure.