Maven分开单元测试和集成测试

Leo*_*nel 27 integration-testing maven maven-surefire-plugin maven-failsafe-plugin

UT =单元测试IT =集成测试.我的所有Integration测试类都使用@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>
Run Code Online (Sandbox Code Playgroud)
  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>
    
    Run Code Online (Sandbox Code Playgroud)
  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>
    
    Run Code Online (Sandbox Code Playgroud)
  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>        
    
    Run Code Online (Sandbox Code Playgroud)

我的结果是:

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 =>好的

khm*_*ise 26

解决方案是这样的:

  <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>
Run Code Online (Sandbox Code Playgroud)

这将让您控制执行的内容.

运行UT和IT:

mvn clean verify
Run Code Online (Sandbox Code Playgroud)

运行IT但不是UT

mvn clean verify -Dsurefire.skip=true 
Run Code Online (Sandbox Code Playgroud)

运行UT而不是IT:

mvn clean verify -DskipITs=true 
Run Code Online (Sandbox Code Playgroud)

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

maven-surefire-plugin的命名约定(单元测试). maven-failsafe-plugin的命名约定(集成测试).