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

时间:2016-05-27 13:46:33

标签: maven integration-testing jacoco jacoco-maven-plugin

我进行了集成测试并且执行得很好,但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>

1 个答案:

答案 0 :(得分:1)

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

build-helper-maven-plugin包括src/it/java中测试类路径的所有类。然后,两个测试插件中的include标签模式(surefire,failsafe)过滤此测试类路径中的类。这些过滤的类由测试插件执行。

所以工作流程是

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