Running tests from maven command line: Different options
When we work on developing automation scripts ,we may sometime need run based on different conditions. Let us see some here.
Assuming Prerequisite met for running :
- Some TestClasses with TestMethods are present in Framework
- Maven is used as build tool for Framework design.
- Using TestNG framework for Test Cases
The following are the steps:
Run specific test from a test class:
mvn -Dtest=Testclassname#testmethodname test
To run all tests in class:
mvn -Dtest=classname test
To run test that match a pattern:This will run all tests that starts with testcreate in class named Testclassname
mvn -Dtest=Testclassname#testcreate* test
Run all test methods match pattern ‘testFoo*’ and ‘testBar*’ from a test class:
mvn test -Dtest=Test1#testFoo*+testBar*
To run multiple methods in a testclass:
mvn -Dtest=Testclassname#testone+testtwo test
To run Multiple method from multiple class :
mvn test -Dtest=CLASS_NAME1#METHOD_NAME1,CLASS_NAME2#METHOD_NAME2
To run multiple test classes:
mvn -Dtest=CLASS_NAME1,CLASS_NAME2
Maven: Exclude specific test(s):
# Exclude one test class, by using the explanation mark (!)
mvn test -Dtest=!LegacyTest
# Exclude one test method
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn test -Dtest=!com.mycompany.app.Legacy*
Run tests in specific xml file using command line:
step1:Add compiler plugin and surefire plugin in pom.xml.
- Adding compiler plugin. Source and Target node values (values as 1.8) represent Java version.

Adding Surefire Plugin. “${suiteXmlFile}” is the path for test suite xml file is to be executed, being passed from command line.

step2:To run test using maven, open Command Line
change directory to project.
Run command mvn clean test -DsuiteXMLFile=testclass2.xml
testclass2.xml is passed to surefire plugin in pom.xml.
-DsuiteXMLFile represents as -D+filename mentioned in surefire plugin.
If testclass2.xml is present in different location other than under project directory , then complete path should be given like “D:\TestFolder\testclass2.xml”.
Run profiles from maven commands:
Let us assume we have two profiles (Regression and Smoke in two different testng.xml files) in pom.xml as below.
To run Regression profile: mvn test -PRegression
This will trigger all tests presetn in testng2.xml file
To run smoke profile: mvn test -PSmoke
This will trigger all tests present in testng.xml file

Please comment if you find anything to add.