配置PIT Maven插件后仍执行IT后缀测试,如何排除集成测试?
如何在PIT Maven插件中排除集成测试(*IT.java)
你遇到的问题其实挺常见的——PIT的excludedTestClasses配置并不支持你用的那种Ant风格文件路径通配符(**/*IT.java),它是基于类的全限定名来匹配的,不是文件系统路径。这就是为什么你的配置没生效的核心原因。
给你两个靠谱的解决方案:
方案一:基于类名/包名的精确匹配
如果你的集成测试类都是以IT结尾(比如de.comp.proj.UserServiceIT),可以直接用类名后缀来匹配排除:
<plugin> <groupId>org.pitest</groupId> <artifactId>pitest-maven</artifactId> <version>1.3.1</version> <configuration> <verbose>true</verbose> <mutationThreshold>80</mutationThreshold> <targetClasses> <param>de.comp.proj.*</param> </targetClasses> <excludedTestClasses> <!-- 匹配所有de.comp.proj包下以IT结尾的类 --> <param>de.comp.proj.*IT</param> </excludedTestClasses> </configuration> </plugin>
如果你的集成测试都放在单独的包(比如de.comp.proj.integration),那直接排除整个包更清晰:
<excludedTestClasses> <param>de.comp.proj.integration.*</param> </excludedTestClasses>
方案二:利用Maven测试插件的划分(更推荐)
通常Maven项目里,单元测试用surefire插件管理,集成测试用failsafe插件管理。PIT默认会运行这两个插件的测试,但你可以通过runFailsafe参数关闭集成测试的执行:
<plugin> <groupId>org.pitest</groupId> <artifactId>pitest-maven</artifactId> <version>1.3.1</version> <configuration> <verbose>true</verbose> <mutationThreshold>80</mutationThreshold> <targetClasses> <param>de.comp.proj.*</param> </targetClasses> <!-- 禁用failsafe插件的集成测试执行 --> <runFailsafe>false</runFailsafe> </configuration> </plugin>
这个方法更符合Maven的最佳实践,不用依赖类名的命名规则,只要你的集成测试是用failsafe管理的,就能直接跳过。
验证配置是否生效
你可以运行Maven命令时加上-X参数开启调试模式,查看PIT日志里的Test classes to exclude部分,确认你的排除规则是否被正确加载和应用。
内容的提问来源于stack exchange,提问作者Oliver
相关产品推荐
相关产品推荐

