如何在集成测试失败时跳过post-integration-test阶段执行?
解决方案:集成测试失败时跳过post-integration-test阶段的Docker停止脚本
要实现你想要的效果——当集成测试失败时保留Docker环境以便排查问题,只需要给exec-maven-plugin的执行配置添加一个条件判断即可,利用Maven内置的测试状态属性来控制任务是否跳过。
直接修改你的配置如下:
<execution> <id>docker-compose down</id> <phase>post-integration-test</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>${docker.src}/stop_docker_compose.sh</executable> <!-- 关键配置:测试失败时跳过该执行任务 --> <skip>${maven.test.failure}</skip> </configuration> </execution>
原理说明
${maven.test.failure}是Maven的内置属性,当集成测试(通常由maven-failsafe-plugin执行)失败时,这个属性会被自动设置为true;测试通过时则为false。- 通过
<skip>${maven.test.failure}</skip>配置,插件会在测试失败时跳过停止Docker Compose的脚本执行,让你的容器保持运行状态,方便你查看日志、检查环境来定位失败原因。 - 当你排查完成后,只需要手动执行
mvn post-integration-test或者直接运行你的stop_docker_compose.sh脚本就能停止容器了。
如果你的项目中没有使用maven-failsafe-plugin而是用了其他测试插件,只要该插件能正确设置maven.test.failure属性,这个配置依然有效。
内容的提问来源于stack exchange,提问作者Frederik Claus
相关产品推荐
相关产品推荐

