Saikat
Saikat

Reputation: 16710

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

Upvotes: 3

Views: 4431

Answers (3)

user13977088
user13977088

Reputation: 1

    This works for me: 
    Tests:
         @Test(groups = "Regression")
         public void testOne(){
         System.out.println("Regression");}
    
         @Test(groups = "Functional")
         public void testTwo() {
         System.out.println("Functional");
         }
    
    use this in pom.xml
    
        <build>
        <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0-M5</version>
          <configuration>
            <suiteXmlFiles>
              <suiteXmlFile>run.xml</suiteXmlFile>
            </suiteXmlFiles>
          </configuration>
        </plugin>
        </plugins>
        </build>'



run.xml file: 
   

     <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
    
        <suite name="Suite1" verbose="1" >
        <test name="TestingGroups" >
            <classes>
                <class name="test.GroupsTesting"/>
            </classes>
        </test>
        </suite>'

command to run Functional test cases :mvn test -Dgroups=Functional

Upvotes: 0

Saikat
Saikat

Reputation: 16710

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.

Upvotes: 0

Naman
Naman

Reputation: 31858

You probably need to group your tests into surefire configuration to do that:-

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <groups>ABC,XYZ</groups>
    </configuration>
</plugin>

and then execute

mvn test -Dgroups=ABC

Upvotes: 2

Related Questions