【发布时间】:2016-01-23 07:49:57
【问题描述】:
UT = 单元测试 IT = 集成测试。我所有的集成测试类都用@Category(IntegrationTest.class) 注释
我的目标是:
mvn clean install => 运行 UT 而不是 IT
mvn clean install -DskipTests=true => 不执行任何测试
mvn clean deploy => 运行 UT 而不是 IT
mvn clean test => 运行 UT 而不是 IT
mvn clean verify => 运行 UT 和 IT
mvn clean integration-test => 运行 IT 和 UT 未执行
mvn clean install deploy => 运行 UT 而不是 IT
pom 属性:
<junit.version>4.12</junit.version>
<surefire-plugin.version>2.18.1</surefire-plugin.version>
<failsafe-plugin.version>2.18.1</failsafe-plugin.version>
-
编译器:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerArgument>-Xlint:all</compilerArgument> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin> -
单元测试:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <configuration> <excludedGroups>com.xpto.IntegrationTest</excludedGroups> </configuration> </plugin> -
集成测试:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>${failsafe-plugin.version}</version> <configuration> <groups>com.xpto.IntegrationTest</groups> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> </goals> <configuration> <includes> <include>**/*.class</include> </includes> </configuration> </execution> </executions> </plugin>
我的结果是:
mvn clean install => 好的
mvn clean install -DskipTests=true => 好的
mvn clean deploy => 运行 UT 而不是 IT
mvn clean test => 好的
mvn clean verify => NOK ... 仅执行 UT,但也需要执行 IT
mvn clean integration-test => NOK ... UT 被执行但不应该被执行,IT 未被执行但应该被执行
mvn clean install deploy => 好的
【问题讨论】:
-
您可以添加一些示例测试吗?也许是 UT 和 IT。我想知道如何命名/组织/注释它们是否是问题的一部分。
-
至少你的部分需求可能不可行......你说“mvn clean integration-test”应该运行 IT 而不 UT,但 maven 生命周期是一条链...当您运行
mvn integration-test时,它会运行生成源、编译、编译测试、测试、打包等,一直到并包括集成测试。无论如何,你的 UT 应该是超快的,那你为什么要跳过它们呢? -
首先我建议使用正确的命名约定来命名单元测试
*Test.java和集成测试*IT.java(请参阅 maven-surefire-plugin/maven-failsafe-plugin 的文档)。此外,如果你做这样的事情:mvn clean install deploy这没有意义,也不了解 maven 构建生命周期。如果您执行mvn deploy,则安装是生命周期的一部分,这意味着只需使用mvn deploy。如果调用mvn clean deploy将运行您的 UT,但不是 IT,则说明您的配置错误。 -
我正在使用 maven-surefire-plugin 的命名约定(单元测试)。 maven-failsafe-plugin(集成测试)的命名约定,如建议的那样,它与 khmarbaise 解决方案完美配合。但是现在我的集成测试没有被 cobertura 插件拦截。有什么建议吗?也许改用 JaCoCo?
标签: maven integration-testing maven-surefire-plugin maven-failsafe-plugin