有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法运行jacoco代码覆盖工具

我有一个maven项目(link),我想对其运行代码覆盖率

我在主项目pom文件上运行了命令mvn test -Pcoverage jacoco:prepare-agent jacoco:report,但没有生成报告。相反,我得到的警告是

[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:report (post-test) @ pulsar-discovery-service ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:prepare-agent (default-cli) @ pulsar-discovery-service ---
[INFO] argLine set to -javaagent:/home/jai1/.m2/repository/org/jacoco/org.jacoco.agent/0.7.7.201606060606/org.jacoco.agent-0.7.7.201606060606-runtime.jar=destfile=/home/jai1/pulsar/pulsar-discovery-service/target/jacoco.exec
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:report (default-cli) @ pulsar-discovery-service ---
[INFO] Skipping JaCoCo execution due to missing execution data file.

有人能建议我如何用这个pom file生成代码覆盖率报告吗。我正在使用apache-maven-3.3.9和testNG


共 (4) 个答案

  1. # 1 楼答案

    下面是一个完整的简单演示项目,展示了单元、集成和测试。测试覆盖率通过Jacoco显示

    • 多模块项目
    • 单元测试(通过mvn清洁安装)
    • 集成测试(通过mvn clean install-P集成测试)
    • Cucumber测试(通过mvn清洁安装)-到演示单元/it测试
    • Jacoco-测试覆盖率(聚合数据文件和聚合报告)
    • FindBugs-代码质量

    享受sipmle demo project

  2. # 2 楼答案

    Your pom.xml包含

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <argLine> -Xmx2G -XX:MaxDirectMemorySize=8G
            -Dio.netty.leakDetectionLevel=advanced</argLine>
        </configuration>
      </plugin>
    

    JaCoCo documentation for ^{} goal

    If your project already defines VM arguments for test execution, be sure that they will include property defined by JaCoCo.

    ... define "argLine" as a Maven property rather than as part of the configuration of maven-surefire-plugin:

      <properties>
        <argLine>-your -extra -arguments</argLine>
      </properties>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <!-- no argLine here -->
        </configuration>
      </plugin>
    
  3. # 3 楼答案

    It seems like you are missing below line in pom.xml 
    <destFile>
       ${project.build.directory}/coverage-reports/jacoco-unit.exec
    </destFile>
    

    请查看以下pom:

     <plugin>
               <groupId>org.jacoco</groupId>
               <artifactId>jacoco-maven-plugin</artifactId>
               <version>0.7.1.201405082137</version>
                <executions>
                      <execution>
                          <id>prepare-jacoco-4-unit-tests</id>
                          <goals>
                               <goal>prepare-agent</goal>
                          </goals>
                          <configuration>
              <!-- Sets the path to the file which contains the execution data. -->
             <destFile>
                 ${project.build.directory}/coverage-reports/jacoco-unit.exec
             </destFile>                                             
             <propertyName>jacocoArgLine</propertyName>
             </configuration>
             </execution>
    
                <execution>
                    <id>create-jacoco-reports-4-unit-tests</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                   <configuration>
               <!-- Sets the path to the file which contains the execution data. -->
              <dataFile>
                  ${project.build.directory}/coverage-reports/jacoco-unit.exec
              </dataFile>
              </configuration>
              </execution>
      </executions>
      </plugin>
    

    然后在maven surefire插件中,应该定义以下内容:

    <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <version>2.17</version>
         <configuration>
             <skipTests>${skipUnitTests}</skipTests>
             <argLine>${jacocoArgLine}</argLine>
         </configuration>
    </plugin>
    
  4. # 4 楼答案

    以下是对我们有效的方法:

    设置一些属性:

    <properties>
        ...
        <!-- versions below are last ones that are Java 6 compatible -->
        <version.jacoco-agent>0.7.4.201502262128</version.jacoco-agent>
        <jacocoArgLine />
        <surefire.base.argLine>-XX:-UseSplitVerifier -XX:MaxPermSize=384m -Xmx1024m -Djava.awt.headless=true</surefire.base.argLine>
        ...
    </properties>
    

    以及coverage简介:

        <profile>
            <id>coverage</id>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <version>${surefire.plugin.version}</version>
                            <configuration>
                                <argLine>${surefire.base.argLine} @{jacocoArgLine}</argLine>
                                <systemPropertyVariables>
                                    <java.awt.headless>true</java.awt.headless>
                                </systemPropertyVariables>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>prepare-agent</goal>
                                    <goal>report</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>