TestNg.xml explained

TestNg.xml explained

The TestNg.xml file is a is a configuration file that is used by the TestNG framework to define, pass parameters to test methods and run tests that are defined in test suites inside the xml file.
TestNg gives us the option to include packages, classes or individual test methods in the test suite defined.

Let`s see how is created and how can we use the TestNG.xml
Go to your project folder in your IDE , right click and choose New File

Name the file testNg.xml and press Finish

You will now see a new file called testNg.xml in your project folder.

Put the following code snipped into the file :

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="My Suite" verbose="1" > 
   <test name="First Test" >
         <class name="test.testExample" />

Please note that test from test.testExample is the package name and the testExample is the class name.
You can change them to match your package name and test class name.

What we have in this xml now is a suite defined.
The suite is defined uisng the ‘suite’ tag and it is named “My Suite”

<suite name="My Suite" verbose="1" > 

Then we have the ‘test’ tag. Please note that this is the tag that i mentioned on when we spoked about the BeforeTest/AfterTest annotation.
The name of the test is given by the ‘name’ attribute and in our case is “First Tests”

<test name="First Test" >

Next we have the ‘class’ tag which contains the class that we want to run. The format is in which is written is ‘packageName.className’ in our example ‘test.testExample’

     <class name="test.testExample" />

Now let`s see how we can run it. We have the option tu run it either via terminal or by using the IDE (Eclipse in our case)

To run it from IDE, you will have to right-click the file in the project folder and select Run As -TestNg suite

Now, after we run it, you will notice a new folder appears in your project folder. This folder is named ‘test-output’ and it contains a HTML report of your run.

If you expand this folder you will find the HTML report as a file called “index.html”

Opening this file you will see the report of your run.