从Maven中排除TestNG组

从Maven中排除TestNG组,maven,testng,surefire,Maven,Testng,Surefire,我有一些缓慢的测试依赖于数据库,我不想每次用Maven构建项目时都运行这些数据库。如前所述,我已将excludedGroups元素添加到pom文件中,但似乎无法使其正常工作 我已经创建了一个最小的项目。以下是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构建项目时都运行这些数据库。如前所述,我已将excludedGroups元素添加到pom文件中,但似乎无法使其正常工作

我已经创建了一个最小的项目。以下是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>


不过,这两个测试仍在运行。我不知道我做错了什么。

根据我的经验,排除组功能只有在您有一组包含组时才起作用。因此,为了实现您想要的功能,您需要将所有测试添加到至少一个组中(通过注释类而不是方法,您可以“轻松地”实现这一点)

例如(仅更改NormalTest)

在您的配置中

        <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>

org.apache.maven.plugins
maven surefire插件
2.4.2
快速的
分贝

我知道这并不明显,但这是testng的工作方式:s。作为旁注,我一直使用外部配置文件来进行testng,而不是pom中的嵌入式配置,因此参数
groups
可能不正确。

我最终创建了外部测试套件:

<!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>


并指定要在配置文件中运行的配置文件:

<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>

org.apache.maven.plugins
maven surefire插件
2.11
src/test/resources/suites/standard.xml


充分的
org.apache.maven.plugins
maven surefire插件
src/test/resources/suites/full.xml

这似乎是Maven(实际上是Surefire)的限制,而不是TestNG。单独使用TestNG,您可以只排除一个组,而不包括任何组。我已经尝试将组添加到所有测试中,并在pom中指定这些组,但这似乎没有任何区别。谢谢,这对我很有效。“groups”和“excludedGroups”按预期工作——它们可以一起使用,也可以单独使用,并按您的预期执行。测试日期:maven 2.2.1、maven surefire插件2.4.3、testng 5.14.6。@pestrella surefire插件的最新版本是2.14。您是如何使用2.43进行测试的?@mR_fr0g,正如我提到的,使用
maven-surefire-plugin
2.4.3进行了测试。这是一个较旧的版本,但在中央maven回购协议中存在某些问题。不知道你从哪里得到的2.43;)
        <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>
<!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>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
    <test name="full">
        <packages>
            <package name="com.test.*" />
        </packages>
    </test>
</suite>
<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>