Jeyabal
Jeyabal

Reputation: 71

TestNG execution by methods as well as groups

I like to execute my tests filtered by methods as well as groups. Is that possible in TestNG?

For example. I have below two TestNG classes with two methods.

Class - SampleJ1
Methods - Test1(group=sanity), Test2(group=regression), Test3, Test4, Test5
Class - SampleJ2
Methods - Test1(group=sanity), Test2(group=regression), Test3, Test4, Test5

My automation framework generates TestNG XML file. If I create XML file with below data, it should execute only sanity group of mentioned methods.

<groups>
  <run>
    <include name="sanity" />
  </run>
</groups>
<test thread-count="12" name="Browser">
  <classes>
    <class name="SampleJ1">
      <include method="Test1"/>
      <include method="Test2"/>
      <include method="Test3"/>
      <include method="Test4"/>
    </class>
    <class name="SampleJ2">
      <include method="Test1"/>
      <include method="Test2"/>
      <include method="Test3"/>
      <include method="Test4"/>
    </class>
  </classes>
</test>

Please let me know if this is possible.

Upvotes: 2

Views: 923

Answers (1)

Grzegorz G&#243;rkiewicz
Grzegorz G&#243;rkiewicz

Reputation: 4586

As far as I understand you want to filter test methods to execute by their names and groups they belong to. Firstly, there is no special solution for this in pure TestNG (otherwise some TestNG experts like @juherr would have answered). Skipping test methods with an implementation of the InvokedMethodListener would leave logs you probably do not want.

I see two options.

In the first one you could implement a @Factory method that could instantiate your test classes with Set<Method> or Set<String> as a constructor's parameter. In each of your test classes you would check whether the method to be executed (or its String representation) is in the Set. @Before annotated or beforeInvocation methods would handle the logic whether or not given method will be executed. Additionally, your testng.xml would specify <groups> elements to run. Again this may have the drawback of leaving Skipped: x in your logs and reports.

The second option would be to make good use of Maven together with its Surefire Plugin. (If you do not use any build tool like Gradle or Maven, then I'm sure you should try). When you test with Maven, you can specify which groups and which test methods have to be executed. Your pom.xml should be much like the following:

<!-- modelVersion, groupId, etc. -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
        <!-- (...) -->
    </plugins>
    <!-- (...) -->
</build>
<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.10</version>
    </dependency>
    <!-- (...) -->
</dependencies>

Say, you have a test class like that:

public class TestClass {

    @Test(groups = {"firstGroup"})
    public void firstMethod(Method method) {
        assertEquals(1, 1);
        System.out.println(method.getName());
    }

    @Test(groups = {"secondGroup"})
    public void secondMethod(Method method) {
        assertEquals(1, 1);
        System.out.println(method.getName());
    }

    @Test(groups = {"secondGroup"})
    public void thirdMethod(Method method) {
        assertEquals(1, 1);
        System.out.println(method.getName());
    }

    @Test(groups = {"secondGroup"})
    public void fourthMethod(Method method) {
        assertEquals(1, 1);
        System.out.println(method.getName());
    }
}

In your testng.xml you can place the information, which groups to execute:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite Name">
    <groups>
        <run>
            <include name="secondGroup"/>
        </run>
    </groups>
    <test name="Default Test Name">
        <classes>
            <class name="TestClass"/>
        </classes>
    </test>
</suite>

And then execute a Maven goal to decide which methods you execute:

mvn clean -Dtest=TestClass#thirdMethod+secondMethod test

Syntax for multiple methods and multiple classes can be found here.
Output:

Running TestClass
secondMethod
thirdMethod
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

The fourth method has not been executed, even though it belongs to the secondGroup. If there are many methods to execute, you may write a custom bash script for it.

It was not possible (at least for me) to filter groups with Maven and methods with TestNG.

Upvotes: 1

Related Questions