Running tests from maven command line: Different options

Jadala Ajay
2 min readJun 18, 2021

--

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 :

  1. Some TestClasses with TestMethods are present in Framework
  2. Maven is used as build tool for Framework design.
  3. 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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Jadala Ajay
Jadala Ajay

Written by Jadala Ajay

8 Years Exp Senior Automation Engineer with expertise on Selenium,RestAsured API,Postman,Cypress,WebdriverIO with prog languages Java,Javascript and Python

Responses (1)

Write a response