【问题标题】:Maven separate Unit Test and Integration TestsMaven 分离单元测试和集成测试
【发布时间】: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>
  1. 编译器:

    <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>
    
  2. 单元测试:

    <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>
    
  3. 集成测试:

    <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


【解决方案1】:

解决办法是这样的:

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.19.1</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
          <version>2.19.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
   <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <skip>${surefire.skip}</skip>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

这将让您控制执行哪个。

运行 UT 和 IT:

mvn clean verify

运行 IT 而非 UT

mvn clean verify -Dsurefire.skip=true 

运行 UT 但不运行 IT:

mvn clean verify -DskipITs=true 

您需要遵循插件的命名约定,这会让生活更轻松。

Naming conventions 用于 maven-surefire-plugin(单元测试)。 Naming conventions 用于 maven-failsafe-plugin(集成测试)。

【讨论】:

  • 现在运行良好。但是现在我的集成测试没有被 cobertura 插件拦截。有什么建议吗?也许改用 JaCoCo?
  • 对于cobertura-maven-plugin,我建议查看您可以使用 JaCoCo 的文档?...
  • 是这样吗? maven-surefire-plugin 只有一个测试目标。它既没有集成测试也没有验证目标。当我运行该示例时,它会引发错误:Could not find goal 'integration-test' in plugin org.apache.maven.plugins:maven-surefire-plugin:2.19.1 among available goals. 我正在使用 Apache Maven 3.6.0
  • 集成测试应该使用 Maven Failsafe 插件运行...maven.apache.org/surefire/maven-failsafe-plugin/index.html
猜你喜欢
  • 2016-10-03
  • 1970-01-01
  • 2011-12-11
  • 2014-11-15
  • 2013-03-18
  • 2012-01-01
  • 2013-10-23
  • 1970-01-01
  • 2020-06-27
相关资源
最近更新 更多