There are basically two ways of getting this done.
Approach #1: Using a beanshell selector
- Make sure you are using the latest released version of TestNG. Its
7.0.0-beta1
as of today.
- Add a dependency on beanshell (below is how you would do it if you were using maven)
<dependency>
<groupId>org.apache-extras.beanshell</groupId>
<artifactId>bsh</artifactId>
<version>2.0b6</version>
</dependency>
- Alter your TestNG suite xml to look like below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="53799427_suite" parallel="false" verbose="2">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[
whatGroup = System.getProperty("group");
shouldRun = Arrays.equals(new String[]{whatGroup}, testngMethod.getGroups());
return shouldRun;
]]>
</script>
</method-selector>
</method-selectors>
<test name="53799427_test">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn53799427.TestClassSample"/>
</classes>
</test>
</suite>
Here the test class com.rationaleemotions.stackoverflow.qn53799427.TestClassSample
looks exactly like the sample you shared.
Now when you run this suite xml file by passing the JVM argument -Dgroup=bat
you will see an output which looks like below (which is what are after)
...
... TestNG 7.0.0-beta1 by Cédric Beust (cedric@beust.com)
...
Am bat
PASSED: batTest
===============================================
53799427_test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
53799427_suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Approach #2: Using a custom method selector
- Make sure you are depending on TestNG
7.0.0-SNAPSHOT
(The reason I say so is because, there was a bug in TestNG which prevented this feature from working properly. I went ahead and fixed it as part of GITHUB-1985. But its yet to be released as of today)
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
To consume the snapshot version, you may need to add a <repository>
tag as shown below to your pom file.
<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
- Now create a custom
org.testng.IMethodSelector
implementation which looks like below:
import java.util.Arrays;
import java.util.List;
import org.testng.IMethodSelector;
import org.testng.IMethodSelectorContext;
import org.testng.ITestNGMethod;
public class FilteringMethodSelector implements IMethodSelector {
@Override
public boolean includeMethod(
IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod) {
String whichGroup = System.getProperty("group", "all");
if ("all".equalsIgnoreCase(whichGroup)) {
return true;
}
boolean isEqual = Arrays.equals(new String[]{whichGroup}, method.getGroups());
if (context != null) {
context.setStopped(true);
}
return isEqual;
}
@Override
public void setTestMethods(List<ITestNGMethod> testMethods) {}
}
- Create a testng suite xml file that looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="53799427_suite" parallel="false" verbose="2">
<method-selectors>
<method-selector>
<selector-class
name="com.rationaleemotions.stackoverflow.qn53799427.FilteringMethodSelector" priority="0"/>
</method-selector>
</method-selectors>
<test name="53799427_test">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn53799427.TestClassSample"/>
</classes>
</test>
</suite>
Here the test class com.rationaleemotions.stackoverflow.qn53799427.TestClassSample
looks exactly like the sample you shared.
Now when you run this suite xml file by passing the JVM argument -Dgroup=bat
you will see an output which looks like below (which is what are after)
...
... TestNG 7.0.0-SNAPSHOT by Cédric Beust (cedric@beust.com)
...
Am bat
PASSED: batTest
===============================================
53799427_test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
53799427_suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================