当前位置: 首页 > 知识库问答 >
问题:

我的Jacoco报告目标与测试阶段无关

劳鹏云
2023-03-14

jacoco-ut.exec文件是在target/coverage-reports/jacoco-ut . exec中创建的,但是看不到任何站点/目录。

下面是Jacoco的配置:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <skipTests>false</skipTests>
      <argLine>${surefireArgLine}</argLine>
      <excludes>
        <exclude>**/integration/*.java</exclude>
      </excludes>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <configuration>
      <skip>false</skip>
      <output>file</output>
      <append>true</append>
    </configuration>
    <executions>
      <execution>
        <id>pre-unit-test</id>
        <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>
    </executions>
  </plugin>

以下是控制台输出:

[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.jacoco:jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (pre-unit-test)
[DEBUG] Style:         Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <address>${jacoco.address}</address>
  <append>true</append>
  <classDumpDir>${jacoco.classDumpDir}</classDumpDir>
  <destFile default-value="${project.build.directory}/jacoco.exec">/home/stephane/dev/java/projects/x/x/x/target/coverage-reports/jacoco-ut.exec</destFile>
  <dumpOnExit>${jacoco.dumpOnExit}</dumpOnExit>
  <exclClassLoaders>${jacoco.exclClassLoaders}</exclClassLoaders>
  <inclBootstrapClasses>${jacoco.inclBootstrapClasses}</inclBootstrapClasses>
  <inclNoLocationClasses>${jacoco.inclNoLocationClasses}</inclNoLocationClasses>
  <jmx>${jacoco.jmx}</jmx>
  <output>file</output>
  <pluginArtifactMap>${plugin.artifactMap}</pluginArtifactMap>
  <port>${jacoco.port}</port>
  <project>${project}</project>
  <propertyName>surefireArgLine</propertyName>
  <sessionId>${jacoco.sessionId}</sessionId>
  <skip default-value="false">false</skip>
</configuration>
[DEBUG] -----------------------------------------------------------------------

[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.jacoco:jacoco-maven-plugin:0.7.6.201602180812:report (post-unit-test)
[DEBUG] Style:         Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <dataFile default-value="${project.build.directory}/jacoco.exec">/home/stephane/dev/java/projects/x/x/x/target/coverage-reports/jacoco-ut.exec</dataFile>
  <outputDirectory default-value="${project.reporting.outputDirectory}/jacoco">/home/stephane/dev/java/projects/x/x/x/target/site/jacoco-ut</outputDirectory>
  <outputEncoding default-value="UTF-8">${project.reporting.outputEncoding}</outputEncoding>
  <project>${project}</project>
  <skip default-value="false">false</skip>
  <sourceEncoding default-value="UTF-8">${project.build.sourceEncoding}</sourceEncoding>
</configuration>
[DEBUG] =======================================================================

[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (pre-unit-test) @ x ---
[DEBUG] Dependency collection stats: {ConflictMarker.analyzeTime=0, ConflictMarker.markTime=1, ConflictMarker.nodeCount=158, ConflictIdSorter.graphTime=0, ConflictIdSorter.topsortTime=0, ConflictIdSorter.conflictIdCount=47, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=4, ConflictResolver.conflictItemCount=108, DefaultDependencyCollector.collectTime=191, DefaultDependencyCollector.transformTime=5}
[DEBUG] org.jacoco:jacoco-maven-plugin:jar:0.7.6.201602180812:

[DEBUG]   Excluded: org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1
[DEBUG]   Excluded: classworlds:classworlds:jar:1.1-alpha-2
[DEBUG]   Excluded: org.apache.maven.wagon:wagon-provider-api:jar:1.0-alpha-6
[DEBUG] Configuring mojo org.jacoco:jacoco-maven-plugin:0.7.6.201602180812:prepare-agent from plugin realm ClassRealm[plugin>org.jacoco:jacoco-maven-plugin:0.7.6.201602180812, parent: sun.misc.Launcher$AppClassLoader@70dea4e]
[DEBUG] Configuring mojo 'org.jacoco:jacoco-maven-plugin:0.7.6.201602180812:prepare-agent' with basic configurator -->
[DEBUG]   (f) append = true
[DEBUG]   (f) destFile = /home/stephane/dev/java/projects/x/x/x/target/coverage-reports/jacoco-ut.exec
[DEBUG]   (f) output = file
[DEBUG]   (f) pluginArtifactMap = {org.jacoco:jacoco-maven-plugin=org.jacoco:jacoco-maven-plugin:maven-plugin:0.7.6.201602180812:, backport-util-concurrent:backport-util-concurrent=backport-util-concurrent:backport-util-concurrent:jar:3.1:compile, org.codehaus.plexus:plexus-interpolation=org.codehaus.plexus:plexus-interpolation:jar:1.11:compile, junit:junit=junit:junit:jar:4.8.2:compile, org.codehaus.plexus:plexus-utils=org.codehaus.plexus:plexus-utils:jar:1.5.6:compile, org.apache.maven.shared:file-management=org.apache.maven.shared:file-management:jar:1.2.1:compile, org.apache.maven.shared:maven-shared-io=org.apache.maven.shared:maven-shared-io:jar:1.1:compile, org.apache.maven.reporting:maven-reporting-api=org.apache.maven.reporting:maven-reporting-api:jar:2.2.1:compile, org.apache.maven.doxia:doxia-sink-api=org.apache.maven.doxia:doxia-sink-api:jar:1.1:compile, org.apache.maven.doxia:doxia-logging-api=org.apache.maven.doxia:doxia-logging-api:jar:1.1:compile, org.apache.maven.reporting:maven-reporting-impl=org.apache.maven.reporting:maven-reporting-impl:jar:2.1:compile, org.apache.maven.doxia:doxia-core=org.apache.maven.doxia:doxia-core:jar:1.1.2:compile, xerces:xercesImpl=xerces:xercesImpl:jar:2.8.1:compile, commons-lang:commons-lang=commons-lang:commons-lang:jar:2.4:compile, commons-httpclient:commons-httpclient=commons-httpclient:commons-httpclient:jar:3.1:compile, commons-codec:commons-codec=commons-codec:commons-codec:jar:1.2:compile, org.apache.maven.doxia:doxia-site-renderer=org.apache.maven.doxia:doxia-site-renderer:jar:1.1.2:compile, org.apache.maven.doxia:doxia-decoration-model=org.apache.maven.doxia:doxia-decoration-model:jar:1.1.2:compile, org.apache.maven.doxia:doxia-module-xhtml=org.apache.maven.doxia:doxia-module-xhtml:jar:1.1.2:compile, org.apache.maven.doxia:doxia-module-fml=org.apache.maven.doxia:doxia-module-fml:jar:1.1.2:compile, org.codehaus.plexus:plexus-i18n=org.codehaus.plexus:plexus-i18n:jar:1.0-beta-7:compile, org.codehaus.plexus:plexus-velocity=org.codehaus.plexus:plexus-velocity:jar:1.1.7:compile, org.apache.velocity:velocity=org.apache.velocity:velocity:jar:1.5:compile, commons-collections:commons-collections=commons-collections:commons-collections:jar:3.2:compile, commons-validator:commons-validator=commons-validator:commons-validator:jar:1.2.0:compile, commons-beanutils:commons-beanutils=commons-beanutils:commons-beanutils:jar:1.7.0:compile, commons-digester:commons-digester=commons-digester:commons-digester:jar:1.6:compile, commons-logging:commons-logging=commons-logging:commons-logging:jar:1.0.4:compile, oro:oro=oro:oro:jar:2.0.8:compile, xml-apis:xml-apis=xml-apis:xml-apis:jar:1.0.b2:compile, org.jacoco:org.jacoco.agent=org.jacoco:org.jacoco.agent:jar:runtime:0.7.6.201602180812:compile, org.jacoco:org.jacoco.core=org.jacoco:org.jacoco.core:jar:0.7.6.201602180812:compile, org.ow2.asm:asm-debug-all=org.ow2.asm:asm-debug-all:jar:5.0.4:compile, org.jacoco:org.jacoco.report=org.jacoco:org.jacoco.report:jar:0.7.6.201602180812:compile}
[DEBUG]   (f) project = MavenProject: com.x:3.2.1 @ /home/stephane/dev/java/projects/x/x/x/pom.xml
[DEBUG]   (f) propertyName = surefireArgLine
[DEBUG]   (f) skip = false
[DEBUG] -- end configuration --
[INFO] surefireArgLine set to -javaagent:/home/stephane/.m2/repository/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=/home/stephane/dev/java/projects/x/x/x/target/coverage-reports/jacoco-ut.exec,append=true,output=file

这就是全部。

更新:我运行了以下命令:mvn jacoco:report -X,它说了一些有趣的东西:

[DEBUG]   (f) dataFile = /home/stephane/dev/java/projects/x/x/x/target/jacoco.exec
[DEBUG]   (f) outputDirectory = /home/stephane/dev/java/projects/x/x/x/target/site/jacoco
[DEBUG]   (f) outputEncoding = UTF-8
[DEBUG]   (f) project = MavenProject: com.x:3.2.1 @ /home/stephane/dev/java/projects/x/x/x/pom.xml
[DEBUG]   (f) skip = false
[DEBUG]   (f) sourceEncoding = UTF-8
[DEBUG] -- end configuration --
[INFO] Skipping JaCoCo execution due to missing execution data file:/home/stephane/dev/java/projects/x/x/x/target/jacoco.exec

它正在寻找一个jacoco。exec文件,当我让它创建一个jacoco ut时。exec文件。

自从创建文件以来,这有点令人惊讶:

$ ll target/coverage-reports/jacoco-ut.exec 
-rw-rw-r-- 1 stephane 2.9M Mar 29 09:47 target/coverage-reports/jacoco-ut.exec

我想我需要在报告配置的某个地方指定这个文件名。

更新:在Jacoco配置中移动上述destFile和dataFile配置后,问题得到解决,并在站点/目录中生成报告。

完整的插件现在看起来像这样:

  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <configuration>
      <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
      <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
      <skip>${skipTests}</skip>
      <output>file</output>
      <append>true</append>
    </configuration>
    <executions>
      <execution>
        <id>pre-unit-test</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <propertyName>surefireArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

尽管插件是在plugins元素中定义的,而不是在pluginManagement元素中定义的,但是为了获得报告,我仍然需要执行以下命令:

mvn jacoco:report

我希望在运行命令时生成报告:

mvn clean install

共有1个答案

昌乐
2023-03-14

使用此配置,使用 mvn 全新安装生成站点应该不会有任何问题。

在site文件夹中有两个不同的文件夹:包含单元测试报告的jacoco和包含集成测试报告的jacoco-it。

JaCoco配置

配置 Jacoco 以公开配置变量以集成单元测试 (jacoco.argLine) 和集成测试 (jacoco.it.argLine),并生成一个包含集成测试报告 (jacoco-it) 的文件夹:

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.4.201502262128</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <excludes>
                            <exclude>**/*Test</exclude>
                        </excludes>
                        <includes>
                            <include>com.vectorsf.*</include>
                        </includes>
                        <propertyName>jacoco.argLine</propertyName>
                    </configuration>
                </execution>

                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>

                <execution>
                    <id>prepare-agent-it</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${project.build.directory}/jacoco-integration.exec</destFile>
                        <excludes>
                            <exclude>**/*IT</exclude>
                        </excludes>
                        <includes>
                            <include>com.vectorsf.*</include>
                        </includes>
                        <propertyName>jacoco.it.argLine</propertyName>
                    </configuration>
                </execution>

                <execution>
                    <id>report-it</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                        <dataFile>${project.build.directory}/jacoco-integration.exec</dataFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>

故障保护配置

        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    <configuration>
                        <redirectTestOutputToFile>true</redirectTestOutputToFile>
                        <includes>
                            <include>**/*IT.java</include>
                        </includes>
                        <excludes>
                            <exclude>**/Application.java</exclude>
                        </excludes>
                        <argLine>${jacoco.it.argLine}</argLine>
                        <encoding>UTF-8</encoding>
                        <runOrder>alphabetical</runOrder>
                    </configuration>
                </execution>
            </executions>
        </plugin>

看看<代码>

可靠的配置

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <redirectTestOutputToFile>true</redirectTestOutputToFile>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
                <excludes>
                    <exclude>**/*IT.java</exclude>
                    <exclude>**/Application.java</exclude>
                </excludes>
                <argLine>
                    ${jacoco.argLine}
                </argLine>
                <!-- Force alphabetical order to have a reproducible build -->
                <runOrder>alphabetical</runOrder>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.15</version>
                </dependency>
            </dependencies>
        </plugin>

同样,看看< code >

 类似资料:
  • 我在单元测试用例中使用Mockito和Power Mockito。当我运行配置文件代码时,我能够生成jacoco报告,但是 当我在测试阶段试图生成Jacoco报告时,我遇到了错误 错误[错误]无法执行目标组织。jacoco:jacoco maven插件:0.8.2:project testproject api上的报告(默认报告):生成jacoco报告时出错:创建报告时出错:分析d:\worksp

  • 我在测试android以创建覆盖率测试报告时遇到问题。我使用./gradlew createDebugCoverageReport命令创建覆盖率报告。已创建报告,但所有测试的结果均为%0。第一个设备已植根,但第二个设备未植根。我们使用这两个设备测试勺子,并查看勺子输出。所以这两个设备一起使用。问题:当我们使用第一个设备(根设备)运行此命令时,将按预期创建所有覆盖率测试结果,但当我们同时使用两个设备

  • 在一个配置了多个应用项目和多个 Library 项目的多项目里,当同时运行所有测试的时候,测试结果整合到一份测试报告中可能是非常有用的。 为了实现这个目的,需要在同一个配置中添加另一个插件。可以通过以下方式添加: buildscript { repositories { jcenter() } dependencies { classpath

  • SonarQube不再显示我用jacoco分析的项目的测试覆盖率。似乎SonarQube有了更新(我发现新规则的日期是5月19日)。我还假设,从那时起,不再评估使用jacoco maven插件收集的测试覆盖率。我见过类似问题的老问题,但那是4年前的事了:JaCoCo SonarQube不兼容版本1007 我将POM更新到最新的插件版本0.8.4,但无济于事。有什么想法吗?我需要在哪里更改一些东西,

  • 问题内容: 从不同的代码库执行业务测试时,我必须获得应用程序的代码覆盖率。 我使用:Maven作为我的构建Jbehave作为我的测试框架。测试是用Java编写的。 我的应用程序是部署在tomcat上的一组war文件。 应用程序代码库与测试代码库是分开的。 为了获得覆盖范围,我遵循以下步骤。 1使用maven编译测试代码。 2将应用程序类从其构建位置($ {app.code.dir} / targe

  • 在我的gradle构建脚本中,我有一个部分说在运行任务时生成测试报告:JacocoTestReport 当我运行任务时,它给我一个错误:无法读取执行数据文件…\build\jamoco\test.exec我如何修复这个错误。当我在完整的项目上进行gradle构建时,我看到测试报告正在生成。