TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

How to create XML File in TestNG | TestNG XML File

How to create XML File in TestNG | TestNG XML File with Testng Tutorial, Introduction, Testng Installation, Features of Testng, Suite Test, Testng Exception Test, Group Test, Plug with Ant, Plug with Eclipse, etc.

<< Back to HOW

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.

public class test
{
   @Test
    public void test1()                      // First test case.
  {
    System.out.println("test1"); 
  } 
  @Test
   public  void test2()                     // Second test case.
{
 System.out.println("test2");
 }}

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

package day1;
import org.testng.annotations.Test;

public class module1 
{
	@Test
	public void test1()
	{
		System.out.println("Hello javaTpoint!!");
	}
    
	@Test
	public void test2()
	{
		System.out.println("JTP Travels");
	}}

test2.java

package day1;

import org.testng.annotations.Test;

public class module2 
{
	@Test
  public void test3()
  {
	  System.out.println("hindi100.com");
  }
}

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.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf