Monday, September 23, 2019

Post#110.How to configure testng.xml File with TestNG


How to configure testng.xml File with TestNG
In any project, you will end up to a place where you need to execute so many test cases on a run. Running a set of test cases together is call executing a Test Suite. Those test cases can be dependent to each other or may have to be executed in a certain order. The main advantage of using TestNG framework in Selenium is its ease of running multiple tests from multiple classes using just one configuration

In TestNG framework, we need to create testng.xml file to create and handle multiple test classes. Testng.xml is an XML file that describes the runtime definition of a test suite. It describes complex test definition while still remain easy to edit.
Using Testng.xml file we can run test available in one or more class file and make them as a single group, hence making test more meaningful, we call them as scenario based testing.

Q. Can I run test without using testng.xml file?
Ans-Well answer is yes, you can run. But once your test become larger and requires configuration of multiple test at one place, the best option is testng.xml.
The testng.xml file will have following hierarchy
  • The root tag of this file is <suite>.
  • <suite> tag can contain one or more <test> tags.
  • <test> tag can contain one or more <classes> tags.
  • <classes> tag can contain one or more <method> tags.

Let’s implement testng.xml File in your project:

Creating testng.xml File
To create testng.xml file, Right click on project folder and Go to New -> File as shown in bellow given image. It will open New File wizard.



In New file wizard, select "DemoTestNG" project and add file name = "testng.xml" as shown in bellow given image and click on Finish button.


It will add testng.xml file under your project folder. Now add bellow given lines in your testng.xml file.

<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNPack1.Program1" />
  </classes>
 </test>
</suite>

Now your testng.xml file will looks like bellow.


testng.xml code Explanation:

<suite> : suite tag defines the TestNG suite. You can give any name to your suite using 'name' attribute. In above given example, We have given "Suite One" to our test suite.
<test> : test tag defines the TestNG test. You can give any name to your test using 'name' attribute. In above given example, We have given "Test One" to our test.
<classes> : classes tag defines multiple class. We can multiple test class under classes tag. In our example we have only one class.
<class> : class tag defines the class name which you wants to consider in test execution. In above given example, we have defined name="TestNGPack1.Program1" where 'TestNGPack1' describes package name and ' Program1' describes class name.
So the above given testng.xml file will execute only Program1 class from TestNGPack1 package.

Executing testng.xml File
To Run

Right click on testng.xml file -> Run As -> Select TestNG Suite as shown in bellow given image.


It will start execution of defined software test class 'Program1' from 'TestNGPack1' package.

When execution completed, you can view test execution HTML report as described earlier. 



No comments:

Post a Comment