Run TestNG tests from Command Line


The previous tutorial has explained the Integration of Selenium with TestNG and the tests are executed through either TestNG Suite or testng.xml. This tutorial explains the steps to run the TestNG Tests through the command line.


  1. Selenium
  2. TestNG
  3. Maven
  4. Java 11
  5. Maven Compiler Plugin
  6. Maven Surefire Plugin

Imagine we need to run the TestNG Tests in CI/CD pipelines like Jenkins or GitLab, then we can’t right click and select TestNG Suite or tesng.xml to run the tests. In such situations, the tests can be executed through command line.

We need to add plugins to pom.xml to compile the test code and then run the tests. To know more about Maven Surefire Plugin for TestNG, refer this blog.


         <!--  Compiler Plugin -->
         <!--  Plugin used to execute tests -->

It is needed to add testng.xml to suiteXmlFile to start the execution of the tests.


Create sample class which has @Test methods. In the example below we have created a class as below:

import static org.testng.Assert.assertTrue;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestNGRunFromCommandLine {
	 WebDriver driver;
	    public void setUp() {
	        driver = new FirefoxDriver();
	    @Test(description = "This test validates title of login functionality", priority = 0)
	    public void verifyLoginPage() {
	        String expectedTitle = driver.findElement(By.xpath("//*[@id='logInPanelHeading']")).getText(); 
	        System.out.println("Title :" + expectedTitle);
	        assertTrue(expectedTitle.equalsIgnoreCase("LOGIN Panel"));
	    @Test(description = "This test validates  successful login to Home page", priority = 1)
	    public void verifyHomePage() {
	        System.out.println("Username Entered");
	        System.out.println("Password Entered");
	        String newPageText = driver.findElement(By.xpath("//*[@id='content']/div/div[1]/h1")).getText();
	        System.out.println("newPageText :" + newPageText);
	        assertThat(newPageText, containsString("Dashboard"));
	    public void teardown() {

The below is the testng.xml file, which will execute all the tests that are available under ‘TestNGRunFromCommandLine‘ class.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name="Suite">
  <test name="Test">
      <class name="TestNG_Demo.TestNGRunFromCommandLine"/>

Below commands are used to execute ‘testng.xml’ file from command line. First, we need to go the place where pom.xml of the project is placed. Then use the mvn compile test to compile the code and execute the TestNG tests.

cd C:\Users\Vibha\Projects\Vibha_Personal\ParallelTestsTestNG
mvn compile test

After executing above command, it should execute the tests that we have specified in testng.xml file. Below is the screen shot after the execution of the tests.

This execution generate various TestNG Reports. We are concerned about emailable-report.html and index.html.


Emailable report is a type of summary report that one can transfer to other people in the team through any medium. Click on option “emailable-report.html”. Click on option web browser. The output reports in TestNG reporting will looks like below:


Index report contains the index-like structure of different parts of the report, such as failed tests, test file, passed test, etc.

Right click on the index.html from the project directory. Select option open with web browser option.

The result will look like this:

Congratulations. This tutorial has explain running the tests of TestNG using Command Line. Happy Learning!!


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 )

Facebook photo

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

Connecting to %s