Steps to create TestNG xml and run test using the same
Source: https://www.softwaretestinghelp.com/testng-example-to-create-testng-xml/
What Is TestNG.Xml?
TestNG.xml file is a configuration file that helps in organizing our tests. It allows testers to create and handle multiple test classes, define test suites and tests.
It makes a tester's job easier by controlling the execution of tests by putting all the test cases together and run it under one XML file. This is a beautiful concept, without which, it is difficult to work in TestNG.
Main things in TestNG xml:Concepts Used In TestNG.xml
#1) A Suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.
Example: <suite name=”Testing Google Apps”>
#2) A Test is represented by <test> and can contain one or more TestNG classes.
Example: <test name=”Regression”>
#3) A Class is a Java class that contains TestNG annotations. Here it is represented by the <class> tag and can contain one or more test methods.
Example
<classes> <class name="Googletest.GmailTest"/> <class name="Googletest.MapsTest"/> <class name="Googletest.ImagesTest"/> </classes>
#4) A Test method is a Java method annotated by @Test methods in the source file.
Example:
public class GmailTest {@Testpublic void LoginTest(){System.out.println("Successfully Logged In");}@Testpublic void LogoutTest(){System.out.println("Successfully Logged Out");}} |
Steps To Create TestNG.Xml File
In TestNG, we have to create the TestNG.xml file to handle multiple test classes. We have to configure our test run, set test dependency, include or exclude any classes, test methods, packages, tests, etc. and set the priority as well in the XML file.
Step1: Right-click on the Project folder, go to New and select ‘File’ as shown in the below image.
Step 2: Add the file name as ‘testng.xml’ as shown in the below image and click on the Finish button.
Step 3: Now you can add the below XML code in your testng.xml file. You can choose your Test suite name and the Test name as per the requirements.
Post providing the required information, the testng.xml file looks as below:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="Testing Google Apps"> <test name="Regression"> <classes> <class name="Googletest.GmailTest"/> <class name="Googletest.MapsTest"/> <class name="Googletest.ImagesTest"/> </classes> </test> <!-- Test --></suite> <!-- Suite --> |
In the above XML file, you can see the sequence of tags properly and accurately. Suite => Test Classes => Class.
Here, the Suite name is <suitename=“Testing Google Apps”>
Test name is <testname=“Regression”>
We can give any name to the Suite and Test in the XML file. But we have to provide the correct name to the classes tag which is a combination of your Package name and the Test Case name.
Package name is Googletest and the test case names are:
<class name=“Googletest.GmailTest”/>
<class name=“Googletest.MapsTest”/>
<class name=“Googletest.ImagesTest”/>
<class name=“Googletest.MapsTest”/>
<class name=“Googletest.ImagesTest”/>
Step 4: Let’s run the xml file. Run the test by right clicking on the TestNG xml file and select Run As -> TestNG Suite.
Once the testng.xml file has run, we can see the results in the console.
Advantages Of TestNG.xml
Major advantages of TestNG.xml file are:
- It provides parallel execution of test methods.
- It allows the dependency of one test method on another test method.
- It helps in prioritizing our test methods.
- It allows grouping of test methods into test groups.
- It supports the parameterization of test cases using @Parameters annotation.
- It helps in Data-driven testing using @DataProvider annotation.
- It has different types of assertions that help in validating the expected results with the actual results.
- It has different types of HTML reports, Extent reports, etc. for a better and clear understanding of our test summary.
- It has listeners who help in creating logs.







Comments
Post a Comment