Example of TestNG configuration XML – testng.xml

0

testng.xml is the main configuration file that defines the suite and tests. suite is top level element in TestNG configuration file and is defined by one XML file.

If you need to have more suites then you need to define separate testng.xml file for each suite like database_testng.xml, feature_testng.xml, performance_testng.xml etc.

Structure of testng.xml

suite is the first element of testng.xml. A suite contains one or more test elements.

Top level structure of testng

Top level structure of testng

 

A test is made of one or more classes and a class is made or one or more methods.

 

TestNG configuration

TestNG configuration

 

TestNG Configuration Example

In this example, I have three tests UnitTesting, FunctionalTesting, UITesting and SomeFeature.

From the name of the tests, you can guess:

  1. UnitTesting contains unit tests.
  2. FunctionalTesting contains functional tests.
  3. UITesting is meant for blackbox testing.
  4. SomeFeature contains feature based tests. These tests may be spread across more than one test classes and grouped by someFeature

Class UnitLevelTesting contains three test cases unitTest1, unitTest2 and unitTest3. unitTest3 belongs to group someFeature.

UnitLevelTesting:

package com.javarticles.testng;

import org.testng.annotations.Test;

public class UnitLevelTesting {    

    @Test
    public void unitTest1() {
        System.out.println("Unit test1");
    }
    
    @Test
    public void unitTest2() {
        System.out.println("Unit test2");
    }
    
    @Test(groups="someFeature")
    public void unitTest3() {
        System.out.println("Unit test3");
    }
}

FunctionalTesting contains the configuration methods like @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeMethod and @AfterMethod. It also contains couple of @Test methods, one of them belongs to group someFeature.

FunctionalTesting:

package com.javarticles.testng;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class FunctionalTesting {
    @BeforeSuite
    public void beforeSuite() {
        System.out.println("beforeSuite");
    }
    
    @BeforeTest
    public void beforeTest() {
        System.out.println("beforeTest");
    }
    
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("beforeMethod");
    }
    
    @Test
    public void scenario1() {
        System.out.println("Scenario1");
    }
    
    @Test(groups="someFeature")
    public void scenario2() {
        System.out.println("Scenario2");
    }
    
    @Test
    public void scenario3() {
        System.out.println("Scenario3");
    }
    
    @AfterMethod
    public void afterMethod() {
        System.out.println("afterMethod");
    }
    
    @AfterTest
    public void afterTest() {
        System.out.println("afterTest");
    }
    
    @AfterSuite
    public void afterSuite() {
        System.out.println("afterSuite");
    }
}

In a UITesting class, test method ui1 belongs to group someFeature.

UITesting:

package com.javarticles.testng;

import org.testng.annotations.Test;

public class UITesting {
    @Test(groups="someFeature")
    public void ui1() {
        System.out.println("UI1 testing");
    }
    
    @Test
    public void ui2() {
        System.out.println("UI2 testing");
    }
        
    @Test
    public void ui3() {
        System.out.println("UI3 testing");
    }
    
    @Test
    public void ui4() {
        System.out.println("UI4 testing");
    }
}

The test configuration has all the levels of testing UnitTesting, FunctionalTesting, UiTesting and the feature level testing SomeFeature. Since fetaure someFeature has tests in all the three classes, the <classes> contains all three level of test classes.

testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite">
	<test name="UnitTesting">
		<classes>
			<class name="com.javarticles.testng.UnitLevelTesting" />
		</classes>
	</test>
	<test name="FunctionalTesting">
		<classes>
			<class name="com.javarticles.testng.FunctionalTesting" />
		</classes>
	</test>
	<test name="UITesting">
		<classes>
			<class name="com.javarticles.testng.UITesting" />
		</classes>
	</test>
	<test name="SomeFeature">
		<groups>
			<run>
				<include name="someFeature" />
			</run>
		</groups>
		<classes>
			<class name="com.javarticles.testng.UITesting" />
			<class name="com.javarticles.testng.UnitLevelTesting" />
			<class name="com.javarticles.testng.FunctionalTesting" />
		</classes>
	</test>
</suite>

Output:

[TestNG] Running:
  C:\javarticles_ws\testNGCfg\testng.xml

beforeSuite
Unit test1
Unit test2
Unit test3
beforeTest
beforeMethod
Scenario1
afterMethod
beforeMethod
Scenario2
afterMethod
beforeMethod
Scenario3
afterMethod
afterTest
UI1 testing
UI2 testing
UI3 testing
UI4 testing
UI1 testing
Unit test3
Scenario2
afterSuite

===============================================
Suite
Total tests run: 13, Failures: 0, Skips: 0
===============================================

TestNG result in Eclipse

TestNG Results

TestNG Results

 

Download Source Code

In this article, I have shown you an example of TestNG configuration. You can download the source code here: testNGCfg.zip

Share.

Leave A Reply