在 jacoco-it 目录中进行集成测试

Have integration tests in jacoco-it directory

我有集成测试,它们执行得很好,但 Jacoco 认为它们是单元测试。如何告诉 Jacoco 将它们视为集成测试并显示它们的图形覆盖率,而不是在 jacoco-ut 目录中,而是在 jacoco-it 目录中?

在 Maven 配置中:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
      <argLine>${surefireArgLine}</argLine>
      <excludes>
        <exclude>**/it/java/*.java</exclude>
      </excludes>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
      <execution>
        <id>add-test-source</id>
        <phase>process-test-sources</phase>
        <goals>
          <goal>add-test-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>src/it/java</source>
          </sources>
        </configuration>
      </execution>
      <execution>
        <id>add-test-resource</id>
        <phase>generate-test-resources</phase>
        <goals>
          <goal>add-test-resource</goal>
        </goals>
        <configuration>
          <resources>
            <resource>
              <directory>src/it/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
      <execution>
        <id>add-it-resources</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.directory}/it-classes</outputDirectory>
          <resources>
            <resource>
              <directory>src/it/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <argLine>${failsafeArgLine}</argLine>
      <testSourceDirectory>src/it/java</testSourceDirectory>
      <testClassesDirectory>${project.build.directory}/it-classes</testClassesDirectory>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <configuration>
      <skip>${maven.test.skip}</skip>
      <output>file</output>
      <append>true</append>
      <excludes>
        <exclude>**/config/*</exclude>
        <exclude>**/dialect/*</exclude>
      </excludes>
    </configuration>        
    <executions>
      <execution>
        <id>pre-unit-test</id>
        <phase>process-test-classes</phase>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
          <propertyName>surefireArgLine</propertyName>
          <includes><include>**/ut/*</include></includes>
        </configuration>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
        </configuration>
      </execution>
      <execution>
        <id>pre-integration-test</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>prepare-agent-integration</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
          <propertyName>failsafeArgLine</propertyName>
          <includes><include>**/it/*</include></includes>
        </configuration>
      </execution>
      <execution>
        <id>post-integration-test</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>report-integration</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

更新:我在 maven-failsafe-plugin 插件中遗漏了这一点:

<executions>
  <execution>       
    <goals>                       
      <goal>integration-test</goal>               
    </goals>                                                  
  </execution>                                                          
</executions> 

添加后如下:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <argLine>${failsafeArgLine}</argLine>
      <includes>
        <include>**/it/**</include>
      </includes>
    </configuration>
    <executions>
      <execution>       
        <goals>                       
          <goal>integration-test</goal>               
        </goals>                                                  
      </execution>                                                          
    </executions>                                                                   
  </plugin>

集成测试报告显示了集成测试。

最终完整配置为:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
      <argLine>${surefireArgLine}</argLine>
      <includes>
        <include>**/ut/**</include>
      </includes>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
      <execution>
        <id>add-test-source</id>
        <phase>process-test-sources</phase>
        <goals>
          <goal>add-test-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>src/it/java</source>
          </sources>
        </configuration>
      </execution>
      <execution>
        <id>add-test-resource</id>
        <phase>generate-test-resources</phase>
        <goals>
          <goal>add-test-resource</goal>
        </goals>
        <configuration>
          <resources>
            <resource>
              <directory>src/it/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <argLine>${failsafeArgLine}</argLine>
      <includes>
        <include>**/it/**</include>
      </includes>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>integration-test</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <configuration>
      <skip>${maven.test.skip}</skip>
      <output>file</output>
      <append>true</append>
      <excludes>
        <exclude>**/config/*</exclude>
        <exclude>**/dialect/*</exclude>
      </excludes>
    </configuration>        
    <executions>
      <execution>
        <id>pre-unit-test</id>
        <phase>process-test-classes</phase>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
          <propertyName>surefireArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
        </configuration>
      </execution>
      <execution>
        <id>pre-integration-test</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>prepare-agent-integration</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
          <propertyName>failsafeArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-integration-test</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>report-integration</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

您误解了 maven-failsafe-plugin 的 include 标记的模式。该模式用于测试 class 路径中 classes 的包名称(另请参阅 https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html)。

build-helper-maven-plugin 将 src/it/java 中的所有 class 包含到测试 class 路径中。然后,两个测试插件(surefire、failsafe)中的 include 标记模式会过滤此测试 classpath 中的 classes。这些过滤后的 classes 由测试插件执行。

所以工作流程是

  1. Build-Helper-Plugin 扩展了必须编译和执行的测试集 classes。因此使用源指令。该source指令中定义的路径位置与Maven项目路径有关。
  2. 编译器插件编译 java classes.
  3. Jacoco-Plugin 应使用单元测试测量生产 classes 中的覆盖率。因此,必须在执行测试之前准备好普通代理。您可以指定在覆盖率测量中应包含或排除哪些生产代码(include/exclude 指令)。这些指令中的模式是基于包的(使用斜线而不是点)。
  4. Surefire-Plugin 执行完全限定 class 名称与 include 指令的模式匹配的测试。该模式使用斜线而不是点。
  5. 现在 Jacoco-Plugin 应该通过集成测试来测量生产 classes 中的覆盖率。因此,必须在执行测试之前准备好集成代理。同样,您可以指定在覆盖率测量中应包含或排除哪些生产代码(include/exclude 指令)。这些指令中的模式是基于包的(使用斜线而不是点)。
  6. Failsafe-Plugin 也一样。它执行完全限定 class 名称与 include 指令的模式匹配的测试。该模式使用斜线而不是点。