TestNG – Run Tests and Suites using Maven

In this testng maven example, we will learn how to execute testng tests and suites using Maven.

1. Maven Dependency

Before diving into different configurations, let’s include the latest version of org.testng:testng in the project configuration.


2. Include Maven-surefire-plugin

Next, we will need to include the Maven-surefire-plugin in the build section of the pom.xml.

  • By default, the surefire plugin executes all tests in the directory 'src/test/java' that follow the default naming convention i.e. their names are of pattern *Test.java.
  • We can include and/or exclude the test classes using different configurations in the suiteXmlFiles element of surefire configuration.

3. Creating Test Suites

A test suite is XML configuration file to group multiple related test classes. Suites help in testing a particular functionality by executing tests only related to that feature.

A typical suite XML file is given below:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="HelloTestNgSuite">
    <test name="HelloWorldTest" preserve-order="true" thread-count="10" parallel="methods">
            <class name="com.howtodoinjava.demo.testng.HelloTestNg"/>
            <class name="com.howtodoinjava.demo.testng.AnotherTest"/>
            <class name="com.howtodoinjava.demo.testng.YetAnotherTest"/>

We can use regular expressions and various inclusion and expulsion patterns in the suite files.

4. Executing Tests and Suites

4.1. Execute All Tests and Suites

The simplest way to execute all testing tests is run the mvn test command:

$ mvn test

4.2. Execute a Specific Test Suite

The easiest and straight-forward way to execute only a single test class or test suite is by excluding/commenting out all unwanted suites from the configuration of maven-surefire-plugin and run the tests using mvn test command. This way, we do not need to setup extra classpath variables in the application and Maven takes care of everything.


         <!-- Comment out suites that we do not want to run-->

Assuming that we have TestNG and other dependent libraries in the classpath, we can run the one or more suite files (separated with a space) as follows:

$ java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...]

4.3. Execute a Specific Test Class

We can execute a specific test class directly from the command line:

$ mvn test -Dtest="com.howtodoinjava.demo.testng.HelloTestNg"

Happy Learning !!

Sourcecode on Github


Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.