将JUnit类与Maven Failsafe插件一起使用

我正在使用JUnit Categories将集成测试与unit testing分开。 Surefire插件配置有效 – 它会跳过使用我的标记接口IntegrationTest注释的测试。

但是,Failsafe插件不会选择集成测试。 我甚至尝试指定junit47提供程序,但是在集成测试阶段运行零测试。

这是pom.xml片段:

 org.apache.maven.plugins maven-failsafe-plugin 2.12    integration-test     com.mycompany.test.IntegrationTest com.mycompany.test.UnitTest    org.apache.maven.surefire surefire-junit47 2.12    

这是日志的故障安全部分:

 [INFO] --- maven-failsafe-plugin:2.12:integration-test (default) @ MyProject.war --- [INFO] Failsafe report directory: /home/stoupa/MyProject/war/target/failsafe-reports [INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider ------------------------------------------------------- TESTS ------------------------------------------------------- Concurrency config is parallel='none', perCoreThreadCount=true, threadCount=2, useUnlimitedThreads=false Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 

提供者org.apache.maven.surefire.junitcore.JUnitCoreProvider是否可以在日志输出中看到正确的一个?

默认情况下,failsafe插件会排除各种文件。 你必须覆盖它。 因此,请将配置部分更改为以下内容:

   **/*.java  com.mycompany.test.IntegrationTest com.mycompany.test.UnitTest  

替代方法

@Categories会给你带来痛苦,因为你必须标记每个集成测试。

尝试创建如下所述的inttests配置文件,并跳过surefire执行。

   inttests  false      org.apache.maven.plugins maven-surefire-plugin  true    org.apache.maven.plugins maven-failsafe-plugin   **/IT*.java       

使用某些字符串对Integration测试进行前缀或后缀,并在包含中给出。 默认情况下,failsafe会将所有以IT为前缀的测试作为集成测试。 参考: 故障安全包含和排除

使用maven profile命令运行它

 mvn verify -P inttests 

注意:在上面提到的方法中,我们需要在构建期间运行unit testing,然后单独进行inttests。

更新:使用故障安全插件的JUnit4类别

  • 2.12 – 分叉的虚拟机终止而没有说再见。 VM崩溃或System.exit调用? https://issues.apache.org/jira/browse/SUREFIRE-827
  • 2.12.1 – 无法在插件工件列表中找到surefire-booter https://issues.apache.org/jira/browse/SUREFIRE-896
  • 2.12.2 – 当模块中没有测试时会出现bug,它会查找failsafe-summary.xml并失败https://issues.apache.org/jira/browse/SUREFIRE-901