Parametrize to Execute TestNG.xml using Maven

Say suppose we have a scenario where we need to execute only a set of TestNG.xml files from the wide range of available xml suite files. It is like we want to run only the specific tests that are in testng.xml files and some times we may again need to kick off other testng.xml files. But for sure we don't want to execute all the suites and wait for them to complete the execution.

One way that we can actually do this by specifying the required testng.xml files in suiteXmlFiles using maven surefire plugin like below. But here when ever you want to execute set of testng.xml files, you have to keep changing them based on your needs. In the previous article we have seen an example to Execute testng.xml using Maven surefire plug-in

<!-- Below plug-in is used to execute single testng.xml file -->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>2.19.1</version>
		<configuration>
			<suiteXmlFiles>
				<suiteXmlFile>mytestng.xml</suiteXmlFile>
			</suiteXmlFiles>
		</configuration>
</plugin>

And the same can be used to run TestNG xml file from you command line, by using the following command:

mvn clean test -DsuiteXmlFile=mytestng.xml

Note that by default, maven surefire plugin will be looking xml files which are located at the root folder of project.

Now let us try to execute multiple testng suite files: -

We can do this in the same way which we have done in the above, but here we will pass multiple testng.xml files as below :-

<suiteXmlFiles>
 <!-- Need to pass testng.xml files as parameters from command line -->
	<suiteXmlFile>mytestng1.xml</suiteXmlFile>
	<suiteXmlFile>mytestng2.xml</suiteXmlFile>
	<suiteXmlFile>mytestng3.xml</suiteXmlFile>
</suiteXmlFiles>

To execute multiple xml files from command line, we can define a variable in suite file configuration and parametrize them from command line like below:

<suiteXmlFiles>
 <!-- Need to pass testng.xml files as argument from command line -->
	<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>

And below is the command to kick off a specified set of suite files by using User Property: surefire.suiteXmlFiles, which lets us to specify "List of TestNG suite xml file locations".

Below is the command to execute only single testng.xml file: -

mvn clean test -Dsurefire.suiteXmlFiles=mytestng.xml

Below is command to parametrize multiple testng.xml files using Maven :-

mvn clean test -Dsurefire.suiteXmlFiles=mytestng1.xml,mytestng2.xml

Example Maven project to parametrize and choose which TestNG xml suites to run from command line.

If you want to execute something like below, first try to create a simple maven project and add packages like below or as you like and execute.

Below image shows the maven project structure with multiple test packages like Patients, Appointments, Visits etc.

Maven Project structure to parametrize

Below Image shows the tests to be verified for Patients related tests. And the same are added to testNG.xml named as 'patienttests.xml'.

Tests to parametrize

Below image shows the maven command to execute 'patienttests.xml' and 'admintests.xml' tests

Maven Tests Results

Below image shows the resulted output after execute above test suites

command to pass Tests

We can pass single testng.xml file or multiple testng.xml file using the same command. This will be very helpfull when you want to execute tests frequently on your machine or to validate results by only executing certain test suites.

Hope this helps you. Please feel free to comment if you have any issues / suggestions on the above article.

Build Tools: 

Comments

Nice article. I have a question though, once you add the parameter to the pom, how do you run tests without a suite argument, e.g. `mvn test`? The build fails for me, because surefire looks for a suite now.
Thanks

I am trying to parameterize testng.xml from Jenkins for a Maven project.
I have the following in my pom.xml

<configuration>
<suiteXmlFiles>
<suiteXmlFile>${testng.xml}</suiteXmlFile>
</suiteXmlFiles>
</configuration>

In Jenkins I have this for Goals: test "-DsuiteXmlFile=testng1.xml"

It is saying it didn't find any testng.xml to run.

Can anybody please help me.
Thanks.

Check if your testng.xml file name is correct and is under the root folder..

Hi,

<suiteXmlFile>${testng.xml}</suiteXmlFile> in this line you have given parameter name : testng.xml whereas in maven command you give -DsuiteXmlFile.
So in pom file update testng.xml to suiteXmlFile, it will work.

Hi,

How to run many testNg XML files parallelly ?

Thanks

Hi,

I need one help regarding running multiple suites in parallel through surefire.
Is it possible for you to have a look onto the below stackoverflow question and respond.
https://stackoverflow.com/questions/58972663/run-multiple-synchronous-an...

Do to complexity of our project, we have few xml files. those are saved in different folder to make it easy to understand and track. So, my question is if xml files is in folder like
"src/test/resources/UI/runnersUI/regressionUI.xml",
"src/test/resources/UI/runnersWeb/regressionWeb.xml"

how can give then in suiteXmlFilesin pom file and how can I run them from TestNG.

Use the below Configuration. and pass the suiteXmlFile as runnersUI/regressionUI.xml, runnersWeb/regressionWeb.xml
<configuration>
<suiteXmlFiles>
<suiteXmlFile>.src/test/resources/UI/${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
<testFailureIgnore>true</testFailureIgnore>
</configuration>

Hi is it possible to pass testNG xml name as mentioned in this article and override some of parmeters from the same testNG xml file through maven?

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.