When you want to run the TestNG test, which you have created, there are two ways to do so.
If you want to run single test case, you can right click on the test, and Run it as TestNG Test. (form Eclipse).
But, if you want to run a set of test cases from various classes, then you need to specify them somewhere, because, you can’t click on each class and run it. Moreover, when running our test cases from the continuous integration tool ( like Jenkins), we need some configuration file which will tell us which test cases to run, if you need to pass some parameters at runtime, then to do that, like wise to achieve something more.
So, In TestNG, all these things are written in a xml file, called as TestNG.xml.
We can configure our test suite utilizing test methods, classes, packages, groups, and so on in single testng.xml file.
TestNG bolster parallel testing, Parameterization (Passing parameters values through XML
configuration), Data Driven Testing(executing same test strategy numerous times utilizing diverse data) .
The testng.xml file allows lots of flexibility in selecting the test classes and test methods to be executed. You can exclude or include test methods or exclude and include groups.
You can specify the listener to be used by all test classes in the testng.xml file
How to create testing.xml file?
Create testng suite file manually as below:
- Run once your project
- Refresh your project
- Now, “test-output” folder will get created under your project – unfold the folder
- Double click the ‘index.html’ file; it will get open in the Eclipse Browser window
- Click on the ‘.xml’ link given on the left panel
- The test suite XML will get opened on the right-side panel
- Take a copy of the XML content
- Right click your project >> New >> File
- Give name of your test suite file (with.xml extension e.g. testng.xml) and click finish
- Paste the content that you copied from the ‘index.html’ and amend your suite file as per your needs
Sample TestNG.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Aayush Corp Test suite">
<listener class-name="com.aayushCorp.utilities.ITestListenerClass" />
<test verbose="2" name="UserTests" >
</test> <!-- Default test -->
</suite> <!-- Default suite -->
TestNG.xml is –
• Controls test runs
• File can have any name (.xml suffix advised
TestNG creates “testng-failed.xml”, for easy re-run.
• Root: <suite> (then <test>/<classes>/<method>)
• Suite can also point to a list of additional xml suite files.
• Some of the available specifications are described next.
Hope This helps !!!!