3

I have some slow tests that rely on the database that I don't want run every time I build my project with Maven. I've added the excludedGroups element to my pom file as explained http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#excludedGroups but I can't seem to get it working.

I've created a minimal project. Here is the pom.xml:

<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>test</groupId>
    <artifactId>exclude</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <excludedGroups>db</excludedGroups>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>5.14</version>
        </dependency>
    </dependencies>

</project>

And these are the two test classes:

public class NormalTest {

    @Test
    public void fastTest() {
        Assert.assertTrue(true);
    }
}

and

public class DatabaseTest {

    @Test(groups={"db"})
    public void slowTest() {
        Assert.assertTrue(false);
    }
}

However both tests still run. I can't figure out what I'm doing wrong.

2 Answers 2

5

I ended up creating external test suits:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
        <test name="standard">
        <groups>
            <run>
                <exclude name="slow" />
                <exclude name="external" />
                <exclude name="db" />
            </run>
        </groups>
        <packages>
            <package name="com.test.*" />
        </packages>
    </test>
</suite>

and

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
    <test name="full">
        <packages>
            <package name="com.test.*" />
        </packages>
    </test>
</suite>

and specifyied which to run in a profile:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.11</version>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/suites/standard.xml</suiteXmlFile>
        </suiteXmlFiles>
     </configuration>
</plugin>

...

<profile>
    <id>fulltest</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/suites/full.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>
3

From my experience, the excluded groups feature works only when you have a set of included groups. So in order to do what you want, you need to add all the tests to at least one group (you can do this "easily" by annotating the class rather than methods).

For example (just changing the NormalTest)

@Test( groups = "fast")
public class NormalTest {

    @Test
    public void slowTest() {
        Assert.assertTrue(true);
    }
}

and in your configuration

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.2</version>
            <configuration>
                <groups>fast</groups>
                <excludedGroups>db</excludedGroups>
            </configuration>
        </plugin>

I know that this is not obvious, but it's the way that testng works :S. As a side note, I've always used an external configuration file for testng rather that the embedded configuration in the pom, so the parameter groups might not be correct.

5
  • 2
    This seems to be a restriction of Maven (Surefire actually), not TestNG. With TestNG alone, you can just exclude a group without including any. Aug 23, 2011 at 4:27
  • I've tried adding groups to all tests and specifying these in the pom but this doesn't seem to make any difference.
    – samblake
    Aug 23, 2011 at 10:46
  • 1
    Thanks, this worked for me. "groups" and "excludedGroups" worked as expected -- they can be used together or on their own and do what you would expect. Tested on: maven 2.2.1, maven-surefire-plugin 2.4.3, testng 5.14.6.
    – pestrella
    Sep 7, 2012 at 13:02
  • @pestrella the latest version of the surefire plugin is 2.14. How did you test with 2.43?
    – mR_fr0g
    Mar 27, 2013 at 14:50
  • @mR_fr0g as I mentioned, tested with maven-surefire-plugin 2.4.3. It's an older version, but certain exists in the central maven repo. Not sure where you got 2.43 from ;)
    – pestrella
    Mar 27, 2013 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.