What is Testng.xml file?
Testng.xml file is a configuration file (XML file) for TestNG in which we can create test suits, test groups, mark tests for parallel execution, add listeners, and pass parameters to test script. It defines the runtime definition of a test suit.
Using testng.xml file, we can also configure multiple tests in a single test suit and run them in a multithreaded environment.
The following code shown in the figure is a simple example of the Testng.xml file.
In the following example, <suite> and <test> are tags. <test> is a child tag of suite which appears only inside a <suite> tag. name and thread-count are attributes. A DOCTYPE specified in a suite file helps TestNG to validate your XML file and make sure that syntax is correct.
XML (Extensible Markup Language) Tags
An XML tag has the same form as an HTML tag. In XML file, each element is delimited with a start tag and an end tag. Start tag begins with < and end tag begins with </. The testng.xml file allows the following tags to define.
The <test> tag contains one or more TestNG classes. The <class> tag contains one or more test methods. The <suite> tag takes only a mandatory attribute name which will be displayed in the reports that TestNG generates.
The mandatory attribute name takes a value in which you can give a suite name “TestNGTestSuite” as shown in the above figure.
The list of all legal attributes that <suite> accepts is as follows.
1. name: It is name of the suite. It is a mandatory attribute.
2. verbose: verbose is an attribute that is mostly used when reporting a bug or when trying to diagnose the execution of test run.
3. parallel: The attribute parallel is used to run multiple tests in parallel. The value that it takes will either be methods or classes. It can be enabled by adding parallel = “methods” or parallel = “classes” to select multiple methods or classes to run in parallel within TestNG. It can also be enabled by adding parallel = “true” and disabled by adding parallel = “false”.
4. thread-count: The thread-count attribute is used to run the maximum number of threads for each suite, if the parallel mode is enabled (otherwise ignore it).
For example, thread-count=”2″: It will run your tests with two threads. This number can be any value that you want.
5. annotations: It is the type of annotations that you are using in your tests.
6. time-out: The time-out attribute is used to declare a time-out period that will be applied to all test methods in the suite.
For example, time-out = “200”: It tells that any test method in this suit takes more than specified time period ( in this case 200 milliseconds) to complete execution, it will be treated as failed.
<test> is a child tag of <suite> tag. It contains one or more TestNG classes that have to run. <test> tag can also be parent of tags that can be declared on <suite>.
For example, <group> tag, <parameter> tag, and <package>. It takes only mandatory attribute name. <test> takes also other attribute such as verbose, parellel, and time-out.
<classes> and <class>
<classes> is a child tag of <test> . It enables you to define Java classes that have to be included in the test run. A class can contain at least one TestNG annotation and one or more test methods. It is represented by the <class> tag. It takes only an attribute name.
The simple example source code of using <classes> tag in a testng.xml file is given below.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="First Test" > <classes> <class name = "class1" /> </classes> </test> <test name = "Second Test"> <classes> <class name = "testngtests.Class2"/> <class name = "testngtests.Class3"/> </classes> </test> </suite>
<packages> and <package>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="Package suite"> <test name="First Test" > <packages> <package name = "test.sample" /> </packages> </test> </suite>
The <exclude> tag represents name of group that has not to be executed. The source code of testng.xml file that contains groups tag is given below.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Test Suite"> <test name="Group Test"> <groups> <run> <exclude name = "GroupTest1" /> <include name = "GroupTest2" /> </run> </groups> <classes> <class name = "testngtests.TestGroupss"/> </classes> </test> </suite>
TestNG will search all classes that are added to the test to include or exclude particular test methods that belong to particular groups.
TestNG will search all test methods that are included or excluded for the test in particular groups. Once found, these test methods will then be executed by TestNG.
The following source code is a simple example of testng.xml file that contains <classes> and <methods> tags.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Test Suite"> <test name="Methods Test"> <classes> <class name = "test.IndividualMethodsTest"> <methods> <include name = "testMethod1" /> <exclude name = "testMethod"2 /> </methods> </class> </classes> </test> </suite>
Use of Testng.xml file
There are following usage of the testng.xml file in TestNG. They are as follows:
1. The testng.xml file can be used to control the execution of whole tests from a single file.
2. We can run a set of test cases from a single place.
3. We can pass parameters to test methods or classes.
4. Using testng.xml file, we can perform parallel test execution.
5. We can add the listener.
How to create TestNG XML File in Eclipse?
There are two ways to create testng.xml file in eclipse. Let’s see the first way. Perform the following steps to create the testng.xml file in eclipse.
Step 1: Open Eclipse and go to the TestNG project that you have created.
Step 2: Select the project folder and then right-click on it. Now go to New option and select File.
Step 3: You will see a File window as shown in below screenshot. Select your TestNG project.
Step 4: Enter the text “myFirstTestNG.xml” in File name section and click on Finish button.
Step 5: Now Eclipse will add a new file to your project folder as shown in below screenshot. It will open the file in the editor.
Step 6: Now add the following snippet code given below in the newly created testng.xml file and save it. This snippet code is just an example. It will be configured according to your test case.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name=" My First Suite" verbose="1" > <test name="First Test" > <classes> <class name="testngtest.FirstTest" /> </classes> </test> </suite>
In this XML file, we have defined a TestNG suite using tag name “suite”. The name of suite is defined as “My First Suite” by using “name” attribute. It contains a test declared using the XML tag test.
The name of this test is defined as “First Test” by using “name” attribute. The test contains a class (testngtest.FirstTest) that is configured by using the classes and class tags in the XML file. This class will be executed for test execution.
Let’s see the second way to create the testng.xml file in the eclipse.
This method is the most preferred and easy way to create testng.xml file in Eclipse. Let’s see the following steps.
Step 1: To create testng.xml file, right-click on the previously created test class “says FirstTest”.
Step 2: Go to TestNG option and select Convert to TestNG option. A new dialog window will open in which you enter the test name “FirstTest”. Now click on the Finish button.
Step 3: You will notice that a new testng.xml file has created below in your project folder.
Step 4: Open it and modify according to your requirements.
Now let’s move ahead and learn how to run previously created testng.xml configuration file.
How to Run TestNG XML File in Eclipse?
There are multiple ways to run the testng.xml configuration file as a TestNG suite.
1. Using command prompt.
2. Using Eclipse.
3. Using IntelliJ
4. By Ant / Maven
5. By Batch File
Using Eclipse to run testng.xml configuration file is the easiest method. Perform the following steps to run testng.xml file through Eclipse:
1. Open Eclipse and go to TestNG project folder where you have created testng.xml file.
2. Select the testng.xml file, right-click on it, and select Run As option and then go to TestNG suite option.
3. The eclipse will execute XML file as TestNG suite. After completion of execution, you can see the following report in Eclipse.
We can also use Run Configuration option provided by Eclipse to customize our TestNG tests in Eclipse. Let’s understand how to configure Eclipse to run testng.xml file.
How to Configure Eclipse to Run Particular TestNG XML File?
Perform the following steps to configure Eclipse for running a particular testng.xml file. They are as follows:
1. Go to Run option on the top bar menu of Eclipse and then select Run Configurations option.
2. Selection TestNG from the set of configurations and click on New Launch Configuration icon.
3. On configuration dialog window give a name “First Test” to the configuration as shown in below screenshot.
4. Now go to the project section, click on the Browse button, and then select your project on the project window.
5. Similarly, go to the Suite section, click on Browser and then select “myFirstTestNG.xml” configuration.
6. Now click on Apply button and then click on Run. It will run your selected testng.xml configuration file.
Hope that this tutorial has covered almost all the important points related to how to create and run testng.xml file in Eclipse with screenshots and step by step. I hope that you will have understood this topic and enjoyed it.
Thanks for reading!!!
Next ⇒ Parameterization in TestNG⇐ PrevNext ⇒