Execution of TestNG Suite (testng.xml)

In this blogpost, we are going to learn how to execute the TestNG test suite using TestNG.

So far, we have seen how to execute a single TestNG class, but in practical, we need to run multiple test classes one after the another, and this can be achieved by creating a TestNG suite, in the form of XML file.

Let us understand the concept using a working example.

Step 1: Create first TestNG class, and name it as TestClass1.java

package com;

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

public class TestClass1 {

@BeforeSuite
public void beforeSuite() {
System.out.println(“Inside Before Suite Method.”);
}

@BeforeClass
public void beforeClass() {
System.out.println(“Inside Before Class of TestClass1.”);
}

@BeforeTest
public void beforeTest() {
System.out.println(“Inside Before Test Method of TestClass1.”);
}

@BeforeMethod
public void beforeMethod1() {
System.out.println(“Inside Before Method 1 of TestClass1.”);
}

@BeforeMethod
public void beforeMethod2() {
System.out.println(“Inside Before Method 2 of TestClass1.”);
}

@Test
public void testMethod1() {
System.out.println(“Inside Test Method 1 of TestClass1.”);
}

@Test
public void testMethod2() {
System.out.println(“Inside Test Method 2 of TestClass1.”);
}

@AfterTest
public void afterTest() {
System.out.println(“Inside After Test Method of TestClass1.”);
}

@AfterMethod
public void afterMethod1() {
System.out.println(“Inside After Method 1 of TestClass1.”);
}

@AfterMethod
public void afterMethod2() {
System.out.println(“Inside After Method 2 of TestClass1.”);
}

@AfterClass
public void afterClass() {
System.out.println(“Inside After Class Method of TestClass1.”);
}

@AfterSuite
public void afterSuite() {
System.out.println(“Inside After Suite Method.”);
}

}

Step 2: Create second TestNG class, and name it as TestClass2.java.

package com;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestClass2 {

@BeforeClass
public void beforeClass() {
System.out.println(“Inside Before Class of TestClass2.”);
}

@BeforeMethod
public void beforeMethod1() {
System.out.println(“Inside Before Method 1 of TestClass2.”);
}

@BeforeMethod
public void beforeMethod2() {
System.out.println(“Inside Before Method 2 of TestClass2.”);
}

@Test
public void testMethod1() {
System.out.println(“Inside Test Method 1 of TestClass2.”);
}

@Test
public void testMethod2() {
System.out.println(“Inside Test Method 2 of TestClass2.”);
}

@AfterMethod
public void afterMethod1() {
System.out.println(“Inside After Method 1 of TestClass2.”);
}

@AfterMethod
public void afterMethod2() {
System.out.println(“Inside After Method 2 of TestClass2.”);
}

@AfterClass
public void afterClass() {
System.out.println(“Inside After Class Method of TestClass2..”);
}

}

Step 3. Create an XML file, and name it as TestNG.xml, and Save it in the project folder.

TestNG_XML_Location

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=”Suite” parallel=”tests”>
<test name=”FirefoxTest”>
<classes>
<class name=”com.TestClass1″ />
<class name=”com.TestClass2″ />
</classes>
</test>
</suite>

Step 4. Right click on TestNG.xml file, Click on Run As, and TestNG Suite. You will get the results as shown in the image below.

TestNG_XML_Result.PNG

That’s all from this blog post, hope it works for you!

Cheers 🙂

Advertisements