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

37 views
Skip to first unread message

Gajanan Mahajan

unread,
Aug 12, 2015, 12:09:49 PM8/12/15
to testng-users
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?

krishnan.ma...@gmail.com

unread,
Aug 12, 2015, 8:59:10 PM8/12/15
to testng...@googlegroups.com

Add alwaysRun=true to your BeforeClass and AfterClass annotations and try again.

-Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else"

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages