Maven Profile 没有运行适当的测试

Posted

技术标签:

【中文标题】Maven Profile 没有运行适当的测试【英文标题】:Maven Profile does not run appropriate tests 【发布时间】:2015-01-26 10:00:37 【问题描述】:

我已经配置了一个只运行集成测试的配置文件,但它仍在运行所有测试。 这是配置:

    <profile>
        <id>integration</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.18.1</version>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <goals>
                                <goal>test</goal>
                            </goals>
                            <phase>test</phase>
                            <configuration>
                                <includes>
                                    <include>**/*IT.java</include>
                                </includes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

我尝试使用正则表达式,就像在 http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html 中解释的那样,但我发现这不起作用。 如何仅使用 IT.java 获得运行该目的的测试?

【问题讨论】:

首先是使用 maven-failsafe-plugin 而不是 maven-surefire-plugin。两个插件都有适当的命名模式(Test.java、*TestCase.java、Test.java 用于surefire,而用于failsafe: *IT.java etc.)。除了在测试阶段运行集成测试是错误的之外,因为存在一个正是为此而设计的集成测试生命周期阶段。此外,这里最好有完整的 pom 文件。 【参考方案1】:

您没有重新配置安全运行,而是在生命周期中添加了另一个。这样,surefire:test 会运行两次,一次使用默认执行 ID (default-test),另一次使用新执行 (integration-test)

如果您只是想更改正在运行的测试,您可以使用:

<profile>
    <id>integration</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <includes>
                        <include>**/*IT.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

这只会重新配置现有的执行。

尽管如此,正如 khmarbaise 在评论中所写的那样,简单地使用 maven-failsafe-plugin 更有意义。如果你真的想跳过正常的单元测试(你为什么要这样呢?),你可以在你的配置文件中明确禁用surefire(使用skipTests-configuration)。

【讨论】:

嗨,谢谢你们两位的helo。 @blackbuild:不,我不想删除单元测试。运行 mvn test 应该只运行单元测试,“mvn test -PsoapUi”应该运行集成的soapUi-Tests,“mvn test -Pintegration”应该只运行以 IT 结尾的测试。我删除了添加的执行并让它运行。

以上是关于Maven Profile 没有运行适当的测试的主要内容,如果未能解决你的问题,请参考以下文章

maven profile

maven profile动态选择配置文件

maven profile动态选择配置文件

maven profile实现多环境打包

项目实现不同环境不同配置文件-maven profile

maven项目之Profile---不同环境打包不同配置