TestNG XML

Before continuing further, we will learn about TestNG XML file in this chapter. TestNG XML is a configuration file used in TestNG. Till now we were using java file to add tests and running those tests using the IDE by running the java file. TestNG xml file is used to to define and customize the execution of test suites, test cases, and test methods. It provides a way to control various aspects of your test runs, such as specifying which classes, methods or groups to include or exclude, setting up parallel execution, configuring listeners for reporting, and passing parameters to tests.


Following are some of the key elements and attributes commonly used in a TestNG XML file:

  • suite: The root element of the XML file, representing a test suite. You can define multiple test suites within a single XML file.
  • test: Represents an individual test case or a set of test cases. You can define multiple test elements within a suite.
  • classes: Specifies the test classes to be included in a test. You can specify one or more classes as child elements.
  • methods: Specifies individual test methods to run. You can include or exclude specific methods.
  • groups: Defines groups of test methods. You can specify which groups to include or exclude.
  • packages: Specifies packages to be included in the test run.
  • parameter: Allows you to pass parameters to test methods. You can define parameters with their names and values.
  • listeners: Specifies listener classes for test events, such as test start, test failure, or test success. Listeners can be used for custom reporting or logging.
  • parallel: Configures parallel execution of tests at the suite, test, class, or method level.
  • suite-files: Allows you to include other TestNG XML files within the current suite.
  • include and exclude: Used within various elements to include or exclude specific test classes, test methods, or groups.

To understand better let's create two class file ExampleTest1.java and ExampleTest2.java

ExampleTest1.java

Java
Copy
package org.example;

import org.testng.annotations.Test;
public class ExampleTest1 {
    @Test
    public void ExampleTest1Method1()
    {
        System.out.println("Example Test1 Method1");
    }
    @Test
    public void ExampleTest1Method2()
    {
        System.out.println("Example Test1 Method2");
    }
}

ExampleTest2.java

Unknown
Copy
package org.example;

import org.testng.annotations.Test;
public class ExampleTest2 {
    @Test
    public void ExampleTest2Method1()
    {
        System.out.println("Example Test2 Method1");
    }
    @Test
    public void ExampleTest2Method2()
    {
        System.out.println("Example Test1 Method2");
    }
}

Now create following xml file called testng.xml


XML
Copy
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="DemoTestSuite">
    <test name="Demo Test">
        <classes>
            <class name="org.example.ExampleTest1"/>
            <class name="org.example.ExampleTest2"/>
        </classes>
    </test>
</suite>

  • We have created suite element and given a name DemoTestSuite.
  • Under the suite we have added test element with name Demo Test
  • Under test we have added classes element and added reference to the classes with full path of the class.

The above xml represents a configuration file which translate to what we are trying to run. You can now run the testng.xml file instead of the java file. This will now run the tests from both the classes.