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

Add new comment