Javatpoint Logo
Javatpoint Logo

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.

Source code

How to create XML file in TestNG

Output

How to create XML file in TestNG

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

  • Right click on the project. Move your cursor down, and you will see TestNG and then click on the Convert to TestNG.
How to create XML file in TestNG
  • The below screen shows the preview of the xml file. Click on the Next button.
How to create XML file in TestNG
  • Click on the Finish button.
How to create XML file in TestNG
  • The testing.xml file is shown below:
How to create XML file in TestNG

In the above source code of xml file, suite is at the higher hierarchy in TestNG. Inside the , you have to define the test name folder. This test name folder is the name of the folder. For example, In a loan company, there are three different types of modules such as personal loan, home loan and car loan, and each module contain its own test cases. All these test cases are defined in the test name folder.

Now we will create the module of personal loan.

Step 1: We first create two java files and both the files contain test cases.

tes1.java

test2.java

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.

How to create XML file in TestNG

Output

How to create XML file in TestNG

In the above output, we observe that all the test cases run successfully without any failure.






Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA