You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用maven-failsafe-plugin执行特定子集的集成测试

解决maven-failsafe-plugin区分执行IT与SpecialIT测试的问题

场景1:执行所有以IT结尾但不含SpecialIT的测试

直接利用插件默认的IT测试匹配规则,补充排除SpecialIT类即可:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.2.5</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <excludes>
            <exclude>**/*SpecialIT.java</exclude>
        </excludes>
    </configuration>
</plugin>

说明:failsafe默认已包含**/*IT.java这类规则,只需排除SpecialIT结尾的类,即可实现需求。

场景2:仅执行以SpecialIT结尾的测试

核心是覆盖插件默认的includes规则,精准指定只匹配SpecialIT类,无需额外排除配置:

<profiles>
    <profile>
        <id>run-special-it</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>3.2.5</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <includes>
                            <include>**/*SpecialIT.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

执行命令:mvn verify -Prun-special-it

说明:之前的错误逻辑是先排除所有IT类(会把SpecialIT也排除),再尝试包含SpecialIT——但maven匹配规则是先按includes筛选候选集,再用excludes移除,因此直接指定includes为SpecialIT路径即可精准执行目标测试。

额外说明

  • 无需修改任何测试类名,完全通过maven配置实现区分
  • 适配JUnit5、JUnit4、Spock混合环境,插件会自动识别不同测试框架的执行引擎(确保已引入对应依赖即可)
  • 建议使用最新稳定版插件,避免旧版本的匹配逻辑bug

内容的提问来源于stack exchange,提问作者Anatoliy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:15:28