Thursday, August 22, 2019

How to Create Testng.xml file




To run tests , handling multiple tests or managing the tests execution in TestN -  we need to create testng.xml file.  we can configure our test run, can set test dependency, can include or exclude any test, method, class or package and set priority etc in the testing.xml file.

1. testing.xml file Creation :
  • Right click on Project folder, go to “New” and select “File” as shown in below image.




  •  In New file wizard, add file name as ‘testng xml‘ as shown in below given image and click on Finish button.



2 :  Writing testng.xml code:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
   <test name="Test">
     <classes>
        <class name="testCases.TestCase_1"/>
     </classes>
   </test> <!-- Test -->
</suite> <!-- Suite -->

Suite: we use this tag to provide the suite name
Test:  this tag will be used to write different tests like sanity, regression etc.
Classes: we can execute specific classes by providing class names

Note: You can choose any name for your Test Suite & Test Name as per your need.
After giving appropriate names, now your testng xml file will looks like this:

The hierarchy in the testng xml file is very simple to understand.
Very first tag is the Suite tag<suite>, under that it is the Test tag<test> and then the Class tag<classes>. You can give any name to the suite and the test but you need to provide the correct name to the <classes> tag which is a combination of your Package name and Test Case name.
Example:

 Package Name is “testCases”Test Case Name is “Testcase_1”. So the Class Name should be testCases.Testcase_1
3. Executing the testng.xml file:
Now let’s run the xml. Run the test by right click on the testng xml file and select Run As > TestNG Suite.



TestNg Result after execution:



No comments:

Post a Comment

If any suggestions or issue, please provide