2

I have a scenario where i need to run my tests by including & excluding certain testng groups.

Consider below scenario

import org.testng.annotations.Test;

public class GroupingTest {

@Test(groups = {"bat"})
public void batTest(){
    System.out.println("Am bat");
}
@Test(groups = {"p1"})
public void p1Test(){
    System.out.println("Am p1");
}
@Test(groups = {"p2"})
public void p2Test(){
    System.out.println("Am p2");
}
@Test(groups = {"bat","p3"})
public void batp3Test(){
    System.out.println("Am bat p3 ");
}
}

Here, how i can run only "bat" test group and it should NOTrun a "bat" test which is also a "33" . In the above case when i run.. it should print only "Am bat" How can i achieve it? Any recommendations?

pranav
  • 57
  • 1
  • 5
  • Already been asked and answered https://stackoverflow.com/questions/1873995/run-a-single-test-method-with-maven – MrKulli Dec 16 '18 at 04:33

1 Answers1

1

There are basically two ways of getting this done.

Approach #1: Using a beanshell selector

  1. Make sure you are using the latest released version of TestNG. Its 7.0.0-beta1 as of today.
  2. 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>
  1. 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

  1. 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>
  1. 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) {}
}
  1. 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
===============================================
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66