当前位置 : 主页 > 网络安全 > 测试自动化 >

单元测试和集成测试

来源:互联网 收集:自由互联 发布时间:2021-06-19
UT =单元测试 IT =集成测试.我所有的集成测试类都用@Category(IntegrationTest.class)注释 我的目标是: mvn clean install =运行UT而不是IT mvn clean install -DskipTests = true =没有执行测试 mvn clean deploy =运
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 =>好

解决办法是:

<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 for maven-surefire-plugin(单元测试).
用于maven-failsafe-plugin(集成测试)的Naming conventions.

网友评论