HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TestNG / How to run testng.xml from maven

How to run testng.xml from maven

In this testng maven example, we will lean how to execute testng tests using maven build file pom.xml. This knowledge is important if you are planning to automate testng tests and making them part of project build process itself.

1. Write TestNG Tests

Below are two tests which 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. Write TestNG Suite

Let’s see the testng suite files testng.xml and suites-test-testng.xml for both tests.

<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 testng suites in maven

Add below pom.xml in 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 systems. The plugin maven-surefire-plugin is 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 used to help in compiling the code and using the particular JDK version for compilation.

4. TestNG maven example

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

C:\somepath\TestNGExamples>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 above pom.xml file to suit your needs.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. SHRAMANA ROY

    March 4, 2020

    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.

  2. Latika

    August 6, 2019

    Hi ,

    I am facing an error Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test while run as Maven test. I have placed my course package at : https://drive.google.com/drive/folders/1YTOw8OW-8W4PWcudJsRmh-dmtgQxvwma?usp=drive_open

    I am facing this issue in all my packages. Please let me know how to resolve the same.

  3. Ravi

    July 19, 2019

    Hi Lokesh,

    This is a good post and helpful. Thank you for that.

    The tests worked fine and when I tried ‘mvn package’ – that worked fine

    [INFO] — maven-jar-plugin:2.4:jar (default-jar) @ sample-maven-build —
    [INFO] Building jar: C:\Users\HearstWi\eclipse-workspace\test_maven\target\sample-maven-build-1.jar

    But, I have this issue in another project and also your project. When I try to run the Jar
    >java -jar sample-maven-build-1.jar, I get an error that “no main manifest attribute, in sample-maven-build-1.jar”

    I tired many other sources how to fix this,
    – To include a Main runner class
    – To include the manifest attribute in the maven build

    Nothing worked for me, I posted a question also in Stackoverflow but no answers.

    Can you suggest some update on your project which will run the produced jar?

    That will be really helpful.

    Thank you,
    Ravi

  4. Girish

    April 23, 2019

    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

    • Pratik Patil

      June 14, 2019

      You need to have maven installed locally if you want to run tests from cmd.

  5. Anay Ashtaputre

    July 24, 2018

    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’

  6. Amit

    October 21, 2016

    Please help resolve.

    [INFO] ————————————————————————
    [INFO] BUILD FAILURE
    [INFO] ————————————————————————
    [INFO] Total time: 2.261 s
    [INFO] Finished at: 2016-10-21T07:17:24-04:00
    [INFO] Final Memory: 29M/332M
    [INFO] ————————————————————————
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test (default-test) on project xxxxxxx: Exception in provider: org/testng/ITestListener: org.testng.ITestListener -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] https://cwiki.apache.org//confluence/display/MAVEN/MojoFailureException
    [root@at-automation4 minerva-dicr-automation]#
    [root@at-automation4 minerva-dicr-automation]#

    • Lokesh Gupta

      October 22, 2016

      Not so useful error log.

  7. pedro

    March 10, 2016

    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 !

  8. Dinesh

    March 2, 2015

    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.

    • Lokesh Gupta

      March 2, 2015

      There is no other possible meaning of “cannot find class in classpath”. It simply means that required classes are not in classpath. Run the command again after updating classpath. https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

      • Dinesh

        March 3, 2015

        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

        • Lokesh Gupta

          March 3, 2015

          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.

Comments are closed on this article!

Search Tutorials

TestNG Tutorial

  • TestNG – Introduction
  • TestNG – Hello World
  • TestNG – Maven
  • TestNG – Annotations
  • TestNG – Expected Exception
  • TestNG – Disable/Ignore Tests
  • TestNG – Parallel Tests
  • TestNG – Dependent Tests
  • TestNG – Timeout Tests
  • TestNG – @Parameters
  • TestNG – @DataProvider
  • TestNG – @Factory
  • TestNG – @DataProvider
  • TestNG – Before and After
  • TestNG – Test Groups

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces