TestNG – Executing testng.xml using Maven

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

1. Write Few Tests

Below are two tests that we have already written in two previous tutorials.

public class RegularExpressionGroupTest
{
	@Test(groups = { "include-test-one" })
	public void testMethodOne() {
		System.out.println("Test method one");
	}

	@Test(groups = { "include-test-two" })
	public void testMethodTwo() {
		System.out.println("Test method two");
	}

	@Test(groups = { "test-one-exclude" })
	public void testMethodThree() {
		System.out.println("Test method three");
	}

	@Test(groups = { "test-two-exclude" })
	public void testMethodFour() {
		System.out.println("Test method Four");
	}
}
public class ParallelSuiteTest
{
	String testName = "";

	@BeforeTest
	@Parameters({ "test-name" })
	public void beforeTest(String testName) {
		this.testName = testName;
		long id = Thread.currentThread().getId();
		System.out.println("Before test " + testName + ". Thread id is: " + id);
	}

	@BeforeClass
	public void beforeClass() {
		long id = Thread.currentThread().getId();
		System.out.println("Before test-class " + testName + ". Thread id is: "
				+ id);
	}

	@Test
	public void testMethodOne() {
		long id = Thread.currentThread().getId();
		System.out.println("Sample test-method " + testName
				+ ". Thread id is: " + id);
	}

	@AfterClass
	public void afterClass() {
		long id = Thread.currentThread().getId();
		System.out.println("After test-method  " + testName
				+ ". Thread id is: " + id);
	}

	@AfterTest
	public void afterTest() {
		long id = Thread.currentThread().getId();
		System.out.println("After test  " + testName + ". Thread id is: " + id);
	}
}

2. Create TestNG Suites

Let’s see the testng suite files testng.xml and suites-test-testng.xml for both tests. Please create suites as necessary for your project.

<suite name="Group of group Suite" verbose="1">
  <test name="Group of group Test">
    <groups>
      <define name="include-group">
        <include name="include-test-one" />
        <include name="include-test-two" />
      </define>
      <define name="exclude-group">
        <include name="test-one-exclude" />
        <include name="test-two-exclude" />
      </define>
      <run>
        <include name="include-group" />
        <exclude name="exclude-group" />
      </run>
    </groups>
    <classes>
      <class name="com.howtodoinjava.groupExamples.RegularExpressionGroupTest" />
    </classes>
  </test>
</suite>
<suite name="Parallel tests" parallel="tests" thread-count="2" >
  <test name="Test One">
    <parameter name="test-name" value="Test One"/>
    <classes>
      <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
    </classes>
  </test>
  <test name="Test Two">
    <parameter name="test-name" value="Test Two"/>
    <classes>
      <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
    </classes>
  </test>
</suite>

3. Add Suites in Maven Configuration

Add below pom.xml in the project root. The below-given file defines different configurations for maven to build the project. The functionality of each section is already mentioned as inline comments in the code.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.test.maven</groupId>
	<artifactId>sample-maven-build</artifactId>
	<version>1</version>
	<name>sample-maven-build</name>
	<build>
		<!-- Source directory configuration -->
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<!-- Following plugin executes the testng tests -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.14.1</version>
				<configuration>
					<!-- Suite testng xml file to consider for test execution -->
					<suiteXmlFiles>
						<suiteXmlFile>testng.xml</suiteXmlFile>
						<suiteXmlFile>suites-test-testng.xml</suiteXmlFile>
					</suiteXmlFiles>
				</configuration>
			</plugin>
			<!-- Compiler plugin configures the java version to be usedfor compiling
				the code -->
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<!-- Dependency libraries to include for compilation -->
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.3.1</version>
		</dependency>
	</dependencies>
</project>

Maven can be used with any of the continuous integration (CI) systems. The maven-surefire-plugin plugin can be used to configure and execute the tests. Here the said plugin is used to configure the testng.xml and suites-test-testng.xml for the TestNG test and generate test reports.

The plugin maven-compiler-plugin has been used to help in compiling the code and using the particular JDK version for compilation.

4. Demo

Now execute the above pom.xml file using the command “mvn test” from the command prompt. You will get the below result.

C:\somepath\TestNGExamples&gt;mvn test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model forcom.test.maven:sample-maven-build:jar:1
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 27, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sample-maven-build 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ sample-maven-build ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\BAML\DFCCUI\workspace_personal\TestNGExamples\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ sample-maven-build ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ sample-maven-build ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\BAML\DFCCUI\workspace_personal\TestNGExamples\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ sample-maven-build ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.14.1:test (default-test) @ sample-maven-build ---
[INFO] No tests to run.
[INFO] Surefire report directory: C:\BAML\DFCCUI\workspace_personal\TestNGExamples\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Test method one
Test method two
Before test Test One. Thread id is: 10
Before test Test Two. Thread id is: 11
Before test-class Test One. Thread id is: 10
Before test-class Test Two. Thread id is: 11
Sample test-method Test One. Thread id is: 10
Sample test-method Test Two. Thread id is: 11
After test-method  Test One. Thread id is: 10
After test-method  Test Two. Thread id is: 11
After test  Test One. Thread id is: 10
After test  Test Two. Thread id is: 11
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.86 sec

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.346s
[INFO] Finished at: Wed Dec 03 12:02:26 IST 2014
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------

Feel free to add/update the above pom.xml file to suit your needs.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

9 thoughts on “TestNG – Executing testng.xml using Maven”

  1. After running the exact code as above, I am getting the same thread number. And one more thing, Whatever changes i am doing in xml file it is not reflecting. Please help me out.

    Reply
  2. Hi,

    I do not have Maven installed locally on Win 10; using POM.xml and maven setup in eclipse for testng framework.
    So, no mvn.bat does not exist on system.

    How do I run the tests from cmd or batch now?

    Regards,
    Girish

    Reply
  3. How we make sure necessary dependent files are being picked during test suite execution thru ‘testng.xml’?
    Lets say, ‘testng.xml’ has details about a class to be executed. The tests in the class need some zip files which should be deployed on the target server for test to execute successfully. How to make sure we specify correct path so that zip files along with class are also picked for execution.

    I am getting this error – Caused by: java.io.FileNotFoundException: null//.zip (No such file or directory). Not sure why it is picking up ‘null’

    Reply
  4. Hi Lokesh,

    Thank you for your great work. I just want to ask you if i have a lot of xml suite files for the entire application like 50 files or more, should i wrote them all in the pom.xml so i can run them. Is there anyway i can run a folder where all my suite xml file are. it would be better.

    Please i need your help on this.

    Thanks !

    Reply
  5. Hi,
    Thanks for the detailed post. It is very helpful.
    I request your help in addressing one of the issues I am come across.
    I have created a pom.xml file having the same contents as mentioned under the section : “Add the steps in Maven pom.xml” in this post.
    I have placed the pom.xml file inside the directory of the project containing the testng.xml file (Project\Runlist directory) where as, the tests are located in : Project\src\test\java). I am hitting an error : cannot find class in classpath on running mvn test . I am finding bit difficult to understand the issue here. If I run the testng.xml from eclipse, (right click on testng.xml -> Run as -> testng), the test runs fine. Can you please help me in understanding the issue here?

    More Details: My eclipse workspace contains 4 projects 1) framework, 2) Page objects 3) Page Operations and 4) Tests Tests under the Tests project are depending on the other 3 projects.

    Reply
      • Thanks Lokesh. I think the things are all right as the tests are running fine if I try to run from eclipse. But why Maven command complaining about class path??
        Thanks,
        Dinesh

        Reply
        • It’s because eclipse include the classes folder path to classpath while running from eclipse automatically. If you are running from command prompt then you need to add path by your own.

          Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.