# How to run a specific group of tests in TestNG from command line?

TestNGRest AssuredDynamic Programming

Group test is a new innovative feature in TestNG, which doesn't exist in JUnit framework. It permits you to dispatch methods into proper portions and perform sophisticated groupings of test methods.

Not only can you declare those methods that belong to groups, but you can also specify groups that contain other groups. Then, TestNG can be invoked and asked to include a certain set of groups (or regular expressions), while excluding another set.

Group tests provide maximum flexibility in how you partition your tests. You do not have to recompile anything if you want to run two different sets of tests back to back.

Groups are specified in your testNG.xml file using the <groups> tag. It can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.

TestNG allows to run the test suites from the command line (cmd), however the following prerequisites must be met for that −

• All the dependent jars should be available inside a project folder. It includes testing.jar, jcommander.jar and any other jars that are used in the test cases.

• The Path of bin or out folder where .class files are stored after compilation.

• Run the command either using testNG.xml or run only a specific class for a certain group.

In this article, we will see how to run specific groups of tests from the command line.

Approach/Algorithm to solve this problem −

• Step 1 − Create a testing class with two different @Test methods and @BeforeMethods with the group name as shown in the following program section.

• Step 2 − Compile the class. It will create an out folder in IntelliJ and bin folder in Eclipse.

• Step 3 − Place all the jar files in the lib folder.

• Step 4 − Now create the testNG.xml as given below.

• Note − testNG.xml takes higher precedence over command line. Therefore, even if groups are mentioned in command line but not properly configured in testNG.xml, then the execution won't be according to groups. All the execution will be happening as per testNG.xml

• Step 5 − Open the cmd.

• Step 6 − Navigate to the project path using cd <project_path>

• Step 7 − Use the following command to run the testNG.xml:

java -cp <path of lib>; <path of out or bin folder> org.testng.TestNG <path of testng>/testng.xml

Or, use the following command to execute only a specific class with groups −

java -cp <path of lib>; <path of out or bin folder> org.testng.TestNG -testclass <ClassName> -groups <groupname>

## Example

The following code shows how to run a specific group of testNG tests from the command line −

src/NewTestngClass.java

import org.testng.annotations.*;
import java.lang.reflect.Method;
public class NewTestngClass {
// test case 1
@Test(groups={"test1", "test2"})
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
}
// test case 2
@Test(groups={"test2"})
public void testCase2() {
System.out.println("in test case 2 of NewTestngClass");
}
@BeforeMethod(groups={"test1", "test2"})
public void name() {
System.out.println("Before Method");
}
}

src/testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
<test name = "test1">
<groups>
<run>
<include name = "test1" />
</run>
</groups>
<classes>
<class name = "NewTestngClass" />
</classes>
</test>
</suite>

## Command to run

1. Run specific groups with testng.xml

java -cp C:\Users\********\IdeaProjects\TestNGProject\lib\*;C:\Users\********\IdeaProjects\TestNGProjectct\out\production\TestNGProject org.testng.TestNG src/testng.xml

Or,

java -cp .\lib\*;.\out\production\TestNGProject org.testng.TestNG src/testng.xml

## Output

BeforeMethod
in test case 1 of NewTestngClass
===============================================
Command line suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

2. Run specific groups with class name without using testng.xml

java -cp .\lib\*;.\out\production\TestNGProjectct org.testng.TestNG -testclass NewTestngClass -groups "test1"

## Output

BeforeMethod
in test case 1 of NewTestngClass
===============================================
Command line suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Updated on 09-Mar-2022 11:21:05