Search code examples
javamaventestngmaven-surefire-plugin

Maven surefire plugin is not picking up groups options for testng


I have a testng case with following annotation -

@Test(groups="groupA", dataProvider="DataSet1")

But when I trigger following maven command it does not execute the test -

mvn test -Dgroups=groupA

All I see in console is this -

...
...
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ abc-proj ---

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.733 sec -      in TestSuite

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

But when I simply run mvn test it executes the test. Not sure why it is behaving like this. I am using Surefire plugin version 2.19.1 and testng version 6.9.9. Any help will be appreciated.

EDIT I am not using testng.xml and just out of curiosity I tried same thing in a small project -> it works. In that project I created a sample class -

import org.testng.annotations.Test;

public class SampleTest {

    @Test(groups = "groupA")
    public void testA() {
        System.out.println("Inside A");
    }

    @Test(groups = "groupB")
    public void testB() {
        System.out.println("Inside B");
    }

}

And the pom.xml is -

... 
<properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.9</version>
        </dependency>
    </dependencies>
</project>

Here the command mvn test -Dgroups=groupA works fine!

EDIT2 When I removed the dataProvider annotation I noticed some different result, console now says -

Tests run: 1, Failures: 0, Errors: 0, Skipped: 1


Solution

  • Found the root cause of this issue. Here testng is running @DataProvider method first, even before running @BeforeClass method and I have a dependency between these two (I assumed @BeforeClass will run first). This, however, does not cause any problem when I run all the tests at once but becomes evident when I try to run test selectively based on the groups.