How to run TestNG from command line?
Last Updated :
23 Aug, 2024
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.
Similar Reads
Run JUnit Test Cases From the Command Line JUnit allows us to test individual pieces of code by creating test cases, typically for methods and classes. Running tests via the command line can be beneficial for CI/CD pipeline scripts or when the environment does not support an IDE.JUnit supports integration with several build tools, such as Ma
5 min read
How to Run Specific TestNG Suite with Maven from Command Line? When using Maven as your build automation tool and TestNG for Java-based testing, you may need to execute particular test suites instead of running every test. Running test subsets according to functionality, environment, or any other criteria can be helpful in this regard. You must set up your Mave
2 min read
How to run TestNG from IntelliJ IDEA? Running TestNG from IntelliJ IDEA is a straightforward process that allows developers to test their Java applications efficiently. IntelliJ IDEA, one of the most popular IDEs, provides seamless integration with TestNG for running unit tests, integration tests, and more. Whether working on small or l
3 min read
How to pass Parameter to testng.xml from Command Line with Maven? In test automation, we run tests across multiple web browsers. The TestNG with Maven allows us to pass the parameters through the command line dynamically; this enables us to control various test environments without editing the code again and again.Step to Pass Parameter to testng.xml from Command
3 min read
How to Ignore a Class in TestNG? In TestNG, ignoring a class can be done using various methods like the @Test(enabled = false) annotation, excluding it in the testng.xml file, or using groups to control test execution. These techniques give you flexibility in managing your test suite, ensuring that only relevant tests are executed.
4 min read