2009/04/09

TestNG and Group Filters

Recently I solved an interesting problem with test groups in TestNG. To describe the problem, let me describe a sample test class with three methods, which are included into two groups.

public class SampleTest {
  @Test(groups = { "groupA" })
  public void testMethod1() {
    System.out.println("1 in A");
  }

  @Test(groups = { "groupA", "groupB" })
  public void testMethod2() {
    System.out.println("2 in A, B");
  }

  @Test(groups = { "groupB" })
  public void testMethod3() {
    System.out.println("3 in B");
  }
}

Usually, TestNG is called using an Ant task like the following one.

<testng excludedGroups="..." groups="..." ... />

What methods can be executed with various groups and excludedGroups setting presents this table:

MethodsgroupsexcludedGroups
1groupAgroupB
1, 2groupA--
2, 3groupB--
3groupBgroupA
1, 2, 3----

This is pretty logical. But wait, there is a combination missing from that table. What about just the method 2, which is in groupA and groupB? To be honest, I did not find an easy way to achieve this. But there is a workaround. First, you must use an external configuration file for TestNG.

<testng classpathref="...">
  <xmlfileset dir="." includes="testng.xml"/>
</testng>

In this file, you can use Beanshell to enable/disable individual methods.

<suite name="TestSuite">
  <test name="SampleTest">
    <method-selectors>
      <method-selector>
        <script language="beanshell"><![CDATA[
          groups.containsKey("groupA") && groups.containsKey("groupB")
        ]]></script>
      </method-selector>
    </method-selectors>
    <classes>
      <class name="com.blogspot.qecafe.tests.SampleTest" />
    </classes>
  </test>
</suite>

The magic line in Beanshell will cause only methods in both groups to be executed.

There is yet another item missing - 1, 3 combination. You can run it in a similar way just like 2.

1 comment:

  1. Great post - TestNG's grouping feature really does save a lot of time and effort - gee - it is so much easier than using JUnit and building your own grouping!

    ReplyDelete

. .