Maven单独的单元测试和集成测试 [英] Maven separate Unit Test and Integration Tests

查看:102
本文介绍了Maven单独的单元测试和集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UT =单元测试 IT =集成测试.我所有的集成测试类都用@Category(IntegrationTest.class)批注

UT = Unit Tests IT = Integration Tests. All my Integration test classes are annotated with @Category(IntegrationTest.class)

我的目标是:

mvn clean install =>运行UT而不是IT

mvn clean install => runs UT and not IT

mvn clean install -DskipTests = true =>不执行测试

mvn clean install -DskipTests=true => no tests are executed

mvn clean deploy =>运行UT而不是IT

mvn clean deploy => runs UT and not IT

mvn clean test =>运行UT而不是IT

mvn clean test => runs UT and not IT

mvn clean verify =>运行UT和IT

mvn clean verify => runs UT and IT

mvn clean integration-test =>未执行IT和UT

mvn clean integration-test => runs IT and UT are not executed

mvn clean install deploy =>运行UT而不是IT

mvn clean install deploy => runs UT and not 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. 编译器:

  1. Compiler:

<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>

  • 单元测试:

  • Unit Tests:

    <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>
    

  • 集成测试:

  • Integration Tests:

    <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 install -DskipTests=true => OK

    mvn clean deploy =>运行UT而不是IT

    mvn clean deploy => runs UT and not IT

    mvn clean test =>确定

    mvn clean verify => NOK ...仅执行UT,但也需要执行

    mvn clean verify => NOK ... only UT are executed but IT also needs to be executed

    mvn clean integration-test => NOK ... UT已执行,不应执行,IT也不应执行,但应执行

    mvn clean integration-test => NOK ... UT are executed and should not and IT aren't executed but should be executed

    mvn clean install deploy =>确定

    推荐答案

    解决方案是这样的:

      <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>
    

    这将让您控制执行哪个.

    This will let you control which is executed.

    运行UT和IT:

    mvn clean verify
    

    运行IT而不运行UT

    running IT's but NOT UT's

    mvn clean verify -Dsurefire.skip=true 
    

    运行UT,但不运行IT:

    running UT but not IT's:

    mvn clean verify -DskipITs=true 
    

    您需要遵循插件的命名约定,这使生活更轻松.

    You need to follow the naming conventions of the plugins which makes life easier.

    命名约定 -plugin(单元测试). 命名约定,用于maven-failsafe-插件(集成测试).

    Naming conventions for maven-surefire-plugin (unit tests). Naming conventions for maven-failsafe-plugin (integration tests).

    这篇关于Maven单独的单元测试和集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆