How to use maven surefire plugin from command-line

Hello friends, gone are the days when test automation execution was triggered from the confines of our laptops/dedicated machines. It is the age of continuous testing now and test automation execution is mostly triggered from command-line by a continuous integration tool like Jenkins/Gitlab etc. If you have felt the need to control which test sets or group of your TestNG tests are executed, need to pass on URL, browser etc. from command-line to your TestNG tests in your maven project, read on.

Some practitioners hard-code URLs and other parameters in TestNG suite XML.

  <test name="Chrome Tests On Mac">
    <parameter name="url" value="http://localhost:8080"></parameter>
    <parameter name="browser" value="chrome"></parameter>
    <parameter name="os" value="mac"></parameter>
      <class name="demoapp.dummytests.DummyTest"></class>
  </test> <!-- Test -->

But with this approach, unless you edit the XML file you will not be able to change the URL, browser or OS. Consider this – On Dev server your application under test most likely will have a different URL. Similarly, on staging and production, most likely the URL will be different. How do you pass on the URL to your code without hard-coding? Well, there may be several ways, but here I am considering a typical Java-Maven-TestNG project.

You can directly call your TestNG suite from commandline but that will not help. You will need ‘Maven surefire plugin’ to map your TestNG suite. The surefire plugin will help in taking your command-line parameters and passing it on to TestNG in appropriate manner. This plugin not only helps you map your TestNG xml with under build or profile in pom.xml, it also helps in controlling and filtering which tests are executed and which are skipped. In the below section I am only focusing on how to control TestNG tests which are to be executed and how to get command-line parameters inside your tests. However, I am not focusing on the set up of surefire plugin and mapping TestNG suite xml to it. Refer to this link for the same.

For the below command

mvn clean test -Dgroups="smoke"

This will execute only those TestNG tests which are marked as

but not

If you are using this command

mvn clean test -Dgroups="smoke" -Dtestnames="TestSet1,TestSet2"

This will execute only those TestNG tests which are marked as

  • which are marked as @Test(groups={"smoke"}
  • Test sets “TestSet1” and “TestSet2” mentioned in TestNGsuite.xml and not “TestSet3”
      <test name="TestSet1">
        <parameter name="os" value="mac"></parameter>
          <class name="demoapp.dummytests.DummyTest1"></class>
      </test> <!-- Test -->
      <test name="TestSet2">
          <class name="demoapp.dummytests.DummyTest2"></class>
      </test> <!-- Test -->
      <test name="TestSet3">
          <class name="demoapp.dummytests.DummyTest3"></class>
      </test> <!-- Test -->

And this is how you may pass URL

mvn clean test -Dgroups="smoke" -Dtestnames="TestSet1,TestSet2" -Durl="http://localhost:8080/DemoApp" -browser="chrome" -Dos="mac"
Inside the test, you just need to use:

String appURL = System.getProperty("url");
String currBrowser = System.getProperty("browser");
String targetOS = System.getProperty("os");

To make your tests more robust, you may check if any of these variables are null. If yes, that means the environment variable(s) was/were not passed on to the test from command-line. In that case, you may default it to some URL from .properties file or TestNGXML etc.

I hope this information was useful for test automation practitioners. Please write your comments in this post.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s