TestNG – Run multiple test classes (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()
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 should i create my testng.xml file. Please reply will be a great help to me.
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??
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.
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
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] ————————————————————————
[INFO] Building vinapi 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[WARNING] The POM for com.google.code.gson:gson:jar:2.2.4 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ vinapi —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/i332939/Documents/workspace/vinapi/src/main/resources
[INFO]
[INFO] — maven-compiler-plugin:2.3.2:compile (default-compile) @ vinapi —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ vinapi —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/i332939/Documents/workspace/vinapi/src/test/resources
[INFO]
[INFO] — maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ vinapi —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] — maven-surefire-plugin:2.18.1:test (default-test) @ vinapi —
[INFO] Surefire report directory: /Users/i332939/Documents/workspace/vinapi/target/surefire-reports
——————————————————-
T E S T S
——————————————————-
Running TestSuite
Request URL : https://jsonplaceholder.typicode.com/posts
Response Code : 200
Request URL : https://jsonplaceholder.typicode.com/posts/2
Response Code : 200
Request URL : https://jsonplaceholder.typicode.com/users
As you can see above 2 tests from PostTests.java executed and 1 test from userTests started and it hanged there only
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 the classes are loaded in to memory and then all the annotations should be executed for order in which they are being called ???
It seems like they classes are executing in alphabetical order .
Please clear my doubts…
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.
I’m not clear with the order here…
on what basis is this order sequence is being followed ?
testFindOrder
testMakeEmptyOrder
testMakeOrder
testUpdateOrder
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.
Hi i want to run an particular test in an suite test which should be defined in .xml file and not in java class.
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?
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!
Install Eclipse TestNG Plugin – http://testng.org/doc/eclipse.html , right click on the test class and run as testng.
I should add I want to execute my test suite in Eclipse!
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
this ligne :!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”
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!
Thanks for your comment, I will add more real use cases.
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?
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.
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.