Alpha
Alpha

Reputation: 14036

TestNG annotations are not respected when run group based tests through command line

I want to run my tests based on their groups through command line with Maven.(means if I say mvn test -Dgroups=sanity, only tests marked with sanity group will be run)

For achieving above, I've following code in pom.xml. Because of following if nothing is provided on command line for group i.e mvn test, it will run tests with regression group

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.maven.test</groupId>
    <artifactId>MavenTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <groups>regression</groups>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.47.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.8</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.18.1</version>
                    <configuration>
                        <groups>${groups}</groups>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

and my test class has BeforeClass and AfterClass annotations:

public class SanityTest {
  @Test(groups={"sanity"})
  public void f() {
    System.out.println("This is sanity test");
  }

  @BeforeClass
  public void beforeClass() {
    System.out.println("This is before class.");
  }

  @AfterClass
  public void afterClass() {
    System.out.println("This is after class.");
  }
}

In order to run sanity tests, I hit mvn test -Dgroups=sanity. Sanity tests does run but AfterClass, BeforeClass methods are not executed.

Its output is:

This is sanity test

actually, here I'm expecting:

This is before class.
This is sanity test
This is after class.

Could someone please help me what needs to be done so that TestNG annotations are also respected?

Upvotes: 2

Views: 2667

Answers (1)

tdrury
tdrury

Reputation: 2338

You need to add the alwaysRun attribute to your @BeforeClass and @AfterClass annotiations:

From the testNG docs: alwaysRun For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to. For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.

Upvotes: 4

Related Questions