如何使用离线仪器生成Jacoco集成测试报告



构建工具:maven

使用离线仪器的原因:删除PowerMock不是一个选项

问题:两者都运行了失败保护和确定火,并生成了报告。但是,jacoco.exec是生成的,但jacoco-it.exec不是。除此之外,离线仪器,覆盖范围和报告效果很好。

这是我使用的Maven插件配置:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>default-instrument</id>
            <goals>
                <goal>instrument</goal>
            </goals>
        </execution>
        <execution>
            <id>default-restore-instrumented-classes</id>
            <goals>
                <goal>restore-instrumented-classes</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report-integration</id>
            <goals>
                <goal>report-integration</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
        <systemPropertyVariables>
            <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
        </systemPropertyVariables>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.15</version>
    <executions>
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <skipTests>${skip.integration.tests}</skipTests>
            </configuration>
        </execution>
    </executions>
</plugin>

要运行测试,我使用Maven Clean安装。

在测试执行结束时,我将获得以下输出:

[INFO] 
[INFO] --- maven-failsafe-plugin:2.15:integration-test (integration-tests) @ elune ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.8:report (default-report) @ elune ---
[INFO] Loading execution data file C:Projectselunetargetjacoco.exec
[INFO] Analyzed bundle 'elune' with 4 classes
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.8:report-integration (default-report-integration) @ elune ---
[INFO] Skipping JaCoCo execution due to missing execution data file.

另一个可能的指针可能是,在运行单元测试后但在集成测试之前,这些类的消除仪器发生。但是我不知道这是对还是错:

[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.8:restore-instrumented-classes (default-restore-instrumented-classes) @ elune ---
[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ elune ---
[INFO] Building jar: C:Projectselunetargetelune-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) @ elune ---
[INFO] 
[INFO] --- maven-failsafe-plugin:2.15:integration-test (default) @ elune ---
[INFO] Failsafe report directory: C:Projectselunetargetfailsafe-reports

任何人都知道为什么jacoco-it.exec不出现?

我认为,与FailSafe插件的集成测试的仪器不被烘烤。还原 - 启动的级别级别的目标默认值是在集成测试阶段运行的准备包阶段:http://maven.apache.org/ref/3.3.9/maven-core/lifecycles.html-因此,将该目标移动到后整合测试阶段可能足够:

<execution>
  <id>default-restore-instrumented-classes</id>
  <phase>post-integration-test</phase>
  <goals>
    <goal>restore-instrumented-classes</goal>
  </goals>
</execution>

这可能就足够了。否则,您也许可以更改Surefire插件的包含模式,以包括集成测试(如果您的情况下似乎合适)。

@rhinoceros.xn这是对我有用的插件配置。但是我使用mvn clean install运行此

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>default-instrument</id>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-restore-instrumented-classes</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>restore-instrumented-classes</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report-integration</id>
                    <goals>
                        <goal>report-integration</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

jacoco-it.exec在我设置短语后未出现。命令行是: MVN CLEAN安装-djacoco.version = 0.7.8 -dfailonError = false -dmaven.test.failure.ignore.ignore = true

我终于发现问题是我没有在maven-faben-fabebafabe-plugin中添加配置jacoco-agent.destfile。

<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.16</version>
        <configuration>
          <systemPropertyVariables>
            <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
          </systemPropertyVariables>
        </configuration>
      </plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.15</version>
        <configuration>
          <systemPropertyVariables>
            <jacoco-agent.destfile>target/jacoco-it.exec</jacoco-agent.destfile>
          </systemPropertyVariables>
        </configuration>
    <executions>
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
    </executions>
</plugin>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco.version}</version>
        <executions>
          <execution>
            <id>default-instrument</id>
            <goals>
              <goal>instrument</goal>
            </goals>
          </execution>
          <execution>
            <id>default-restore-instrumented-classes</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>restore-instrumented-classes</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
</plugins>

最新更新