当前位置:   article > 正文

集成测试,单元测试隔离 maven-surefire-plugin_maven 集成测试

maven 集成测试

maven的goal生命周期

Maven生存周期 - 含 integration-test

Maven本身支持的命令(Goals)是有顺序的,越后面执行的命令,会将其前面的命令和其本身按顺序执行一遍,具体的顺序如下所示:

  1. validate
  2. initialize
  3. generate-sources
  4. process-sources
  5. generate-resources
  6. process-resources
  7. compile
  8. process-classes
  9. generate-test-sources
  10. process-test-sources
  11. generate-test-resources
  12. process-test-resources
  13. test-compile
  14. process-test-classes
  15. test
  16. prepare-package
  17. package
  18. pre-integration-test
  19. integration-test
  20. post-integration-test
  21. verify
  22. install

从上面maven的生存期可以看出



作者:哈比猪
链接:https://www.jianshu.com/p/e638d64b6955/
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

maven单元测试与集成测试

  1. 通过maven的Profile
  2. 配置生命周期 通过maven-surefire-plugin的生命周期配置不同的测试范围

如下使用的是方式2

unit包中包含的是单元测试

integration包种包含的是集成测试

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-surefire-plugin</artifactId>
  4. <version>2.9</version>
  5. <configuration>
  6. <skip>true</skip>
  7. </configuration>
  8. <executions>
  9. <execution>
  10. <id>run-integration-test</id>
  11. <phase>integration-test</phase>
  12. <goals>
  13. <goal>test</goal>
  14. </goals>
  15. <configuration>
  16. <skip>false</skip>
  17. <includes>
  18. <include>**/integration/**/*.java</include>
  19. </includes>
  20. </configuration>
  21. </execution>
  22. <execution>
  23. <id>run-test</id>
  24. <phase>test</phase>
  25. <goals>
  26. <goal>test</goal>
  27. </goals>
  28. <configuration>
  29. <skip>false</skip>
  30. <includes>
  31. <include>**/unit/**/*.java</include>
  32. </includes>
  33. </configuration>
  34. </execution>
  35. </executions>
  36. </plugin>
  37. <plugin>
  38. <groupId>org.apache.maven.plugins</groupId>
  39. <artifactId>maven-war-plugin</artifactId>
  40. <version>2.0</version>
  41. <configuration>
  42. <dependentWarExcludes>WEB-INF/lib</dependentWarExcludes>
  43. </configuration>
  44. </plugin>
  45. </plugins>

maven-failsafe-plugin 配置POM.XML

实际上POM.XML提供了很强大的配置功能,这里利用一个maven的插件,叫maven-failsafe-plugin,具体配置如下:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-failsafe-plugin</artifactId>
  4. <version>2.17</version>
  5. <executions>
  6. <execution>
  7. <id>integration-tests</id>
  8. <goals>
  9. <goal>integration-test</goal>
  10. <goal>verify</goal>
  11. </goals>
  12. <configuration>
  13. <excludes>
  14. <exclude>none</exclude>
  15. </excludes>
  16. <includes>
  17. <include>**/*IT.java</include>
  18. </includes>
  19. </configuration>
  20. </execution>
  21. </executions>
  22. </plugin>

这里创建了一个execution,当执行integration-test goal的时候,执行包括“IT.java”结尾的所有java文件,那如何跳过unit test,integration-test呢?看下面利用了另外一个插件,叫maven-surefire-plugin,如下配置:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-surefire-plugin</artifactId>
  4. <version>2.6</version>
  5. <configuration>
  6. <skip>false</skip>
  7. </configuration>
  8. </plugin>

通过skip标签来指定跳过test的行为,默认是支持skipITsskipTests,前者是跳过integration-test,后者是跳过所有的测试(稍微说明下,跳过测试方法有很多种,比如maven-failsafe-plugin本身也支持加入skip选项,只不过实现之后每个命令的含义就略微有不同了,大家选择自己最习惯的就好了).

  •  推荐的测试目录结构

  1. Unit Test Classes : src/test/java/**/**Test.java
  2. Integration Test Classes : src/test/java/**/**IT.java
  3. TestCases dependent files: src/test/resources/*

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/331791
推荐阅读
相关标签
  

闽ICP备14008679号