为何maven-surefire-plugin的exclude规则在GitHub中不生效?求替代方案
解决方法
不用exclude规则的话,有几个实用的方案:
用JUnit条件注解控制
在你要跳过的那个集成测试类或方法上,加上JUnit的条件注解,比如:@DisabledIfSystemProperty(named = "github.run", matches = "true") public class YourIntegrationTest { // 测试方法 }然后在GitHub Actions的Maven命令里,加上系统属性参数:
mvn test -Dgithub.run=true本地运行时不加这个参数,测试正常执行;GitHub环境下触发时,这个测试就会被跳过,且它依然属于failsafe的管理范围。
借助Maven Profile
在pom.xml里定义一个专门给GitHub用的Profile,在这个Profile里配置surefire插件,通过test参数指定要运行的测试,间接排除目标测试:<profiles> <profile> <id>github</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <!-- 运行所有测试,除了指定的那个集成测试 --> <test>**/*Test.java,!**/YourIntegrationTest.java</test> </configuration> </plugin> </plugins> </build> </profile> </profiles>然后在GitHub Actions里激活这个Profile:
mvn test -Pgithub本地运行不加
-Pgithub,测试全部正常执行。用测试标签(Tag)过滤
给所有需要在GitHub环境运行的集成测试加上@Tag("keep")注解,然后在GitHub的Maven命令里指定只运行带这个标签的测试:mvn test -Dgroups=keep要是不想给其他测试加标签,也可以反过来给要跳过的测试加
@Tag("skip-github"),然后在GitHub命令里排除这个标签:mvn test -DexcludedGroups=skip-github本地运行时不加这些参数,所有测试都正常跑。
内容的提问来源于stack exchange,提问作者Bohdan Khmil
相关产品推荐
相关产品推荐

