如何配置scalatest-maven-plugin,通过Maven分阶段运行Scala测试?
用scalatest-maven-plugin分离单元测试与集成测试的配置方案
scalatest-maven-plugin不支持maven-surefire/failsafe插件那样的<includes>/<excludes>标签,但可以用它专属的<tests>和<excludeTests>参数实现测试分离,逻辑和你之前的Java项目配置完全一致。针对你的命名规则(*Test.scala为单元测试,*IntegrationTest.scala为集成测试),具体配置如下:
完整POM配置代码
<build> <plugins> <plugin> <groupId>org.scalatest</groupId> <artifactId>scalatest-maven-plugin</artifactId> <version>2.0.1</version> <!-- 建议使用最新稳定版本 --> <executions> <!-- 单元测试:绑定到test阶段,仅运行*Test.scala,排除集成测试 --> <execution> <id>run-unit-tests</id> <phase>test</phase> <goals> <goal>test</goal> </goals> <configuration> <!-- 指定要运行的单元测试文件路径 --> <tests>**/*Test.scala</tests> <!-- 排除集成测试文件 --> <excludeTests>**/*IntegrationTest.scala</excludeTests> </configuration> </execution> <!-- 集成测试:绑定到integration-test阶段,仅运行*IntegrationTest.scala --> <execution> <id>run-integration-tests</id> <phase>integration-test</phase> <goals> <goal>test</goal> </goals> <configuration> <!-- 仅指定集成测试文件路径 --> <tests>**/*IntegrationTest.scala</tests> </configuration> </execution> <!-- 可选:绑定verify阶段,确保集成测试执行成功后才完成构建 --> <execution> <id>verify-integration-tests</id> <phase>verify</phase> <goals> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
配置说明与使用
<tests>:指定要执行的测试文件路径模式,支持通配符**(匹配任意层级目录)和*(匹配任意字符)<excludeTests>:从<tests>匹配的结果中排除指定路径的测试文件- 执行命令:
- 仅运行单元测试:
mvn test - 仅运行集成测试:
mvn integration-test - 运行所有测试并验证结果:
mvn verify
- 仅运行单元测试:
内容的提问来源于stack exchange,提问作者Stormcloud
相关产品推荐
相关产品推荐

