Open In App

How to run TestNG from command line?

Last Updated : 23 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Running TestNG from the command line is a powerful technique for managing your automated tests efficiently. TestNG is a popular testing framework that provides various features for running and configuring tests. While IDE-based execution is common, using the command line offers greater flexibility and is essential for integrating tests into CI/CD pipelines.

In this guide, we’ll walk you through the steps to run TestNG tests from the command line, covering setup, execution, and understanding command line options.

Primary Terminologies

  • TestNG: A Java language testing framework that has several inherent mechanisms to control how test cases are executed.
  • JUnit: Another Java-based testing framework on which the author based his work on when designing TestNG.
  • XML Configuration: The way in which TestNG allows configuring tests using XML files where users can specify test suites, test groups among other things.
  • JUnit/TestNG Integration: The use of TestNG annotations in marking methods as test methods and managing their execution process.
  • Command Line Interface (CLI): An operating system or software interaction interface that operates via commands issued in textual format.

Step-by-Step Guide on Using TestNG via Command Line

Step 1. JDK installation

Ensure you have installed JDK on your computer. You can download it from the official Java website.

Step 2. Installation of TestNG

You will need to incorporate TestNG into your project. This can be done by Maven or including the TestNG JAR file in your project manually.

If using Maven, add the following dependency to your pom.xml:

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.0</version> <!-- Use the latest version -->
<scope>test</scope>
</dependency>

If using Gradle

Add the following to your build.gradle file:

testImplementation 'org.testng:testng:7.7.0' // Use the latest version

Step 3. Create TestNG Test Class:

Create a Java class with TestNG annotations like @Test, @BeforeSuite, and @AfterSuite. Here is an example:

Java
import org.testng.annotations.Test;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

public class SampleTest {
    @BeforeSuite
    public void beforeSuite() {
        System.out.println("Before Suite");
    }

    @Test
    public void testMethod1() {
        System.out.println("Test Method 1");
    }

    @Test
    public void testMethod2() {
        System.out.println("Test Method 2");
    }

    @AfterSuite
    public void afterSuite() {
        System.out.println("After Suite");
    }
}

Step 4. TestNG XML Configuration File (testng.xml):

XML
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="Test">
        <classes>
            <class name="com.example.SampleTest"/>
        </classes>
    </test>
</suite>

Step 5. Compile Command:

javac -cp "path/to/testng.jar" -d bin src/com/example/SampleTest.java

Step 6. Run Command:

java -cp "path/to/testng.jar;bin" org.testng.TestNG testng.xml

Output:

Before Suite
Test Method 1
Test Method 2
After Suite

Understanding the TestNG Command Line Options

TestNG provides several command line options to customize test execution:

  • -d or --disable-suites : Disables specific suites.
  • -testclass : Specifies a test class to run.
  • -suite : Specifies the suite XML file to run.
  • -parallel : Executes tests in parallel.

Conclusion

Mastering how to run TestNG from the command line enhances your testing workflow by providing flexibility and integration capabilities. Whether you're using Maven, Gradle, or running TestNG directly, understanding these command line techniques allows you to execute your automated tests efficiently and seamlessly integrate them into your development pipeline. By leveraging these methods, you can ensure your tests are executed consistently and reliably, improving the overall quality of your software.


Next Article

Similar Reads