TestNG – Run multiple test classes (suite test)

testng-suite-test

In this tutorial, we will show you how to run multiple TestNG test cases (classes) together, aka suite test.

1. Test Classes

Review following three test classes.

TestConfig.java

package com.mkyong.testng.examples.suite;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

//show the use of @BeforeSuite and @BeforeTest
public class TestConfig {

	@BeforeSuite
	public void testBeforeSuite() {
		System.out.println("testBeforeSuite()");
	}

	@AfterSuite
	public void testAfterSuite() {
		System.out.println("testAfterSuite()");
	}

	@BeforeTest
	public void testBeforeTest() {
		System.out.println("testBeforeTest()");
	}

	@AfterTest
	public void testAfterTest() {
		System.out.println("testAfterTest()");
	}

}
TestDatabase.java

package com.mkyong.testng.examples.suite;

import org.testng.annotations.Test;

public class TestDatabase {

	@Test(groups = "db")
	public void testConnectOracle() {
		System.out.println("testConnectOracle()");
	}

	@Test(groups = "db")
	public void testConnectMsSQL() {
		System.out.println("testConnectMsSQL");
	}

	@Test(groups = "db-nosql")
	public void testConnectMongoDB() {
		System.out.println("testConnectMongoDB");
	}

	@Test(groups = { "db", "brokenTests" })
	public void testConnectMySQL() {
		System.out.println("testConnectMySQL");
	}

}
TestOrder.java

package com.mkyong.testng.examples.suite;

import org.testng.annotations.Test;
 
public class TestOrder {
 
	@Test(groups={"orderBo", "save"})
	public void testMakeOrder() {  
	  System.out.println("testMakeOrder");
	}  
 
	@Test(groups={"orderBo", "save"})
	public void testMakeEmptyOrder() {  
	  System.out.println("testMakeEmptyOrder");
	}  
	
	@Test(groups="orderBo")
	public void testUpdateOrder() {  
		System.out.println("testUpdateOrder");
	}  
 
	@Test(groups="orderBo")
	public void testFindOrder() {  
		System.out.println("testFindOrder");
	}  
	
}

2. Testng.xml

To run above test classes, create a XML file – testng.xml (can be any filename) file, and define detail like this :

testng.xml

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

<suite name="TestAll">

	<test name="order">
		<classes>
			<class name="com.mkyong.testng.examples.suite.TestConfig" />
			<class name="com.mkyong.testng.examples.suite.TestOrder" />
		</classes>
	</test>

	<test name="database">  
		<classes>
			<class name="com.mkyong.testng.examples.suite.TestConfig" />
			<class name="com.mkyong.testng.examples.suite.TestDatabase" />
		</classes>
	</test>

</suite>

Output


[TestNG] Running:
  C:\mkyong_projects\TestNG\src\test\resources\testng-all.xml

testBeforeSuite()

testBeforeTest()
testFindOrder
testMakeEmptyOrder
testMakeOrder
testUpdateOrder
testAfterTest()

testBeforeTest()
testConnectMongoDB
testConnectMsSQL
testConnectMySQL
testConnectOracle()
testAfterTest()

testAfterSuite()

3. Other Examples

Here are some common use examples.

3.1 Specify package names instead of class names:

testng.xml

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

<suite name="TestAll">

	<test name="order">
		<packages>
			<package name="com.mkyong.testng.examples.suite.*" />
		</packages>
	</test>

</suite>

3.2 Specify methods to include or exclude :

testng.xml

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

<suite name="TestAll">

  <test name="order">
	<classes>
		<class name="com.mkyong.testng.examples.suite.TestConfig" />
		<class name="com.mkyong.testng.examples.suite.TestOrder">
			<methods>
				<include name="testMakeOrder" />
				<include name="testUpdateOrder" />
				<!-- 
					<exclude name="testMakeOrder" />
				 -->
			</methods>
		</class>
	</classes>
  </test>
 
</suite>

Output


[TestNG] Running:
  C:\mkyong_projects\TestNG\src\test\resources\testng.xml

testBeforeSuite()
testBeforeTest()
testMakeOrder
testUpdateOrder
testAfterTest()
testAfterSuite()

3.3 Specify groups to include or exclude :

testng.xml

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

<suite name="TestAll">

  <test name="database">
	<groups>
		<run>
			<exclude name="brokenTests" />
			<include name="db" />
		</run>
	</groups>
  
	<classes>
		<class name="com.mkyong.testng.examples.suite.TestDatabase" />
	</classes>
  </test>
 
</suite>

Output


[TestNG] Running:
  C:\mkyong_projects\TestNG\src\test\resources\testng.xml

testConnectMsSQL
testConnectOracle()

References

  1. TestNG – testng.xml file

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
17 Comment threads
5 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
17 Comment authors
PurvishivaniSudha VRameshSidhartha Mahato Recent comment authors
newest oldest most voted
trackback
TestNG Tutorials | Tutorials

[…] Tutorial 5 – Suite Test Bundle a few unit test cases and run it together. […]

Purvi
Guest
Purvi

It’s a good article. Thank you so much. i am developing a hybrid framework of an application where around 10 modules and when we mouse hover on it it gives 5-6 options/link/new page after clicking each of it i have to a write a script for it. Say example User Management module having -4 option EditProfile page, Chang Password Page, AboutPage and LogOut page. Same way each of module have same way options/link or page. How do i create testNg.xml file. Should i create individual package for each module? and execute each module/package . Just give me an idea how… Read more »

shivani
Guest
shivani

Hi mkyong,
i have two classes(class1.java and class2.java) in XML file. class1.java has groups and class2.java only contains one @test without group. After run the XML file as TestNG suite, it runs only class1.java but it does not run class2.java at all. Can you please help me here??

Sudha V
Guest
Sudha V

Hi Mkyong,
Is it possible to use a single method again n again in testngxml?For example i have a login method in a separate class and i want to call that method before executing the each class in testing.i tried but its not running.

Ramesh
Guest
Ramesh

I have one problem in TestNG. I have 4 @Test methods, For some reasons, i am calling 3 @Test methods from the first method. Because of that i am getting only one output of 1st @Test method in TestNG Report.
Any solution to get all method results in the Test NG Report. I heard that Java Reflections will do that. Please provide a solution with code.

Sidhartha Mahato
Guest
Sidhartha Mahato

Within a single package I have multiple classes . But how to schedule the execution of classes one after another in testng .
My testng.xml file

But execution is happening in the following order.My testng output is coming in following order

DataWrangler.Register
DataWrangler.Login
DataWrangler.LoginResigterComponents

how to make execution schedule

DataWrangler.LoginResigterComponents
DataWrangler.Register
DataWrangler.Login

Vinay Varma
Guest
Vinay Varma

hey mkyoung, first of all thanks for explaining very clearly. its very easy to find solutions following your blog. I am having a weird problem I am having 2 test classes – PostTests and UserTests. I specified both of them in testng.xml file and when running using maven, I am seeing that all the methods of 1st specified class in testng are running and it hangs….class 2 methods are not running. its hanging Can you please help me what am i doing wrong below is my testng xml Below is output of execution [INFO] Scanning for projects… [INFO] [INFO] ————————————————————————… Read more »

Amit singh
Guest
Amit singh

Today I faced one issue with execution order of @BeforeSuite method. Could you please help me in understanding this concepts. I am trying for testNG annotations And I am facing weired scenario: I have two java classes names as SmokeClassOne.java and RegressionClassTwo.java each class have @BeforeSuite annotated method. From testng.xml I am trying to call these both the classes from test nade as: OUTPUT: @BeforeSuite Methods of both the classes are executing very first. But I am seeing that @BeforeSuite method from RegressionClassTwo is executing first then @BeforeSuite of SmokeClassOne class. Why execution order is changed ?? I think all… Read more »

Samit
Guest
Samit

I have testng.xml similar to the one in “2” above where each test section has a test class and a config class with @BeforeTest methods. Now when I run the test through testng plugin for eclipse the methods annotated with @BeforeTest do not run. How do I get them to run and then run a single test?

Any help would be greatly appreciated.

SRICHANDAR KARPURAM
Guest
SRICHANDAR KARPURAM

I’m not clear with the order here…

on what basis is this order sequence is being followed ?

testFindOrder
testMakeEmptyOrder
testMakeOrder
testUpdateOrder

Guruprasath
Guest
Guruprasath

I created different class files and created Test Suit but it is not running. i don’t know why?. Browser itself is not getting open. Please help me to resolve this problem. Below is my XML code.


emmanuel angelo
Guest
emmanuel angelo

Hi i want to run an particular test in an suite test which should be defined in .xml file and not in java class.

Guest
Guest
Guest

I have @BeforeSuite and @AfterSuite in class A and @Test in ClassB. If am using maven to run the tests. If I don’t add @BeforeClass or @BeforeMethod in class B, tests never get ran. Any idea why this could be?

Brian
Guest
Brian

Hi and thanks for the examples! How am I to get example 5 to run? Once I create an xml file what am I to do with it and how do I execute the suite? Thanks!

Brian
Guest
Brian

I should add I want to execute my test suite in Eclipse!

mkyong
Guest
mkyong

Install Eclipse TestNG Plugin – http://testng.org/doc/eclipse.html , right click on the test class and run as testng.

Lisa
Guest
Lisa

i have imported a project to Android Studio.

In this ligne:

i get the error:
URL is not registered(Settings | Project Settings | Schemas and DTDs)

What should i do?

thanks

Lisa
Guest
Lisa

this ligne :!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”

someoneWhoCaresForReadableCode
Guest
someoneWhoCaresForReadableCode

Hey MKyong,
thanks for posting all this technical stuff. But I think it would contribute a lot to the universal code quality if you cared more for setting up examples that have a meaning and some real use case. Reading your examples is quite difficult because they do not conjure any real image. I think that focusing only on the technical concepts and leaving aside any good practices for clean code pollutes the internet with bad code. Stop that!

mkyong
Guest
mkyong

Thanks for your comment, I will add more real use cases.

anvita
Guest
anvita

I have two separate classes – A and B where i have unit tests. I now have to Make a class C from where I can call tests of A and B. How should I do it?

mkyong
Guest
mkyong

Why you want to create a class C to launch the unit tests of class A and B? In TestNG, create a suite test and include the class A and B, refer to above tutorial.