使用 Cobertura 和 Jacoco 运行代码覆盖率

2024-03-27

我在获取 Maven 插件项目(使用调用程序插件进行集成测试)的 Sonar 中的集成测试和单元测试的代码覆盖率报告时遇到了一些问题。

我无法使用默认的 Jacoco 覆盖率工具进行单元测试,因为这些工具使用 Powermock,这会导致使用该工具的类的覆盖率为 0%。另一方面,如果不使用 Jacoco,我无法找到可靠的方法来获取基于 Groovy 的集成测试的结果。

所以我需要的是 Cobertura 生成单元测试报告,Jacoco 生成集成测试报告,Sonar 能够读取批次。

我尝试使用这里的例子https://github.com/Godin/sonar-experiments/tree/master/jacoco-examples/maven-invoker-plugin-example https://github.com/Godin/sonar-experiments/tree/master/jacoco-examples/maven-invoker-plugin-example但消除了与测试阶段绑定的执行,但随后我在声纳中得到了​​“-”的单元测试覆盖率。我认为原因是为了让这种方法发挥作用,我需要将 Jacoco 指定为 Sonar 的核心覆盖工具。

有什么办法解决这个问题吗?我的 pom.xml 如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.acme.myproj.plugins</groupId>
  <artifactId>slice2java-maven-plugin</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>maven-plugin</packaging>

  <name>Slice2Java Maven Plugin</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <sonar.exclusions>**/generated*/*.java</sonar.exclusions>
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.itReportPath>${project.basedir}/target/jacoco-it.exec</sonar.jacoco.itReportPath>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.codehaus.plexus</groupId>
      <artifactId>plexus-utils</artifactId>
      <version>3.0.8</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
      <dependency>
          <groupId>org.mockito</groupId>
          <artifactId>mockito-all</artifactId>
          <version>1.9.5</version>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.powermock</groupId>
          <artifactId>powermock-core</artifactId>
          <version>1.5</version>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.powermock</groupId>
          <artifactId>powermock-module-junit4</artifactId>
          <version>1.5</version>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.powermock</groupId>
          <artifactId>powermock-api-mockito</artifactId>
          <version>1.5</version>
          <scope>test</scope>
      </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <goalPrefix>slice2java</goalPrefix>
          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
        </configuration>
        <executions>
          <execution>
            <id>mojo-descriptor</id>
            <goals>
              <goal>descriptor</goal>
            </goals>
          </execution>
          <execution>
            <id>help-goal</id>
            <goals>
              <goal>helpmojo</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>run-its</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-invoker-plugin</artifactId>
            <version>1.8</version>
            <configuration>
              <debug>true</debug>
              <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
              <pomIncludes>
                <pomInclude>*/pom.xml</pomInclude>
              </pomIncludes>
              <postBuildHookScript>verify</postBuildHookScript>
              <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
              <settingsFile>src/it/settings.xml</settingsFile>
              <goals>
                <goal>clean</goal>
                <goal>test-compile</goal>
              </goals>
            </configuration>
            <executions>
              <execution>
                <id>integration-test</id>
                <goals>
                  <goal>install</goal>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.5.3.201107060350</version>
                <configuration>
                    <includes>com.acme.*</includes>
                </configuration>
                <executions>
                    <execution>
                        <id>pre-integration-test</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${project.build.directory}/jacoco-it.exec</destFile>
                            <propertyName>invoker.mavenOpts</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>

由于您已将声纳配置为

 <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
 <sonar.jacoco.itReportPath>
     ${project.basedir}/target/jacoco-t.exec
 </sonar.jacoco.itReportPath>

这意味着您正在告诉 Sonarreuse现有报告来自sonar.jacoco.itReportPath。如果没有现有报告,则没有任何覆盖范围。

就我而言,我使用 Cobertura 并重用其报告Maven 站点生成,使用以下配置属性:

<sonar.java.coveragePlugin>cobertura</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.surefire.reportsPath>
    ${project.build.directory}/surefire-reports
</sonar.surefire.reportsPath>
<sonar.cobertura.reportPath>
    ${project.build.directory}/site/cobertura/coverage.xml
</sonar.cobertura.reportPath>

我可以使用以下命令来重用:

mvn clean install site sonar:sonar

我可以使用以下命令重现您的问题:

mvn clean install sonar:sonar

覆盖率为0%。由于目前还没有报告报告路径.

然后请确保有一个名为“jacoco-t.exec”如执行声纳之前指定的那样。

由于我不熟悉 JaCoCo,也不知道哪个 Maven 阶段生成报告文件,所以我建议执行如下命令:

mvn clean test sonar:sonar

or

mvn clean install sonar:sonar

或者和我的一样

mvn clean install site sonar:sonar

我希望这会有所帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Cobertura 和 Jacoco 运行代码覆盖率 的相关文章

随机推荐

  • 使用 sed 为选项卡添加背景颜色?

    是否可以使用 sed 更改选项卡 或任何其他文本 的背景颜色 以便 例如 我可以运行如下所示的内容 somefunction sed e s some pattern set bg color 1 unset bg color g Yes
  • QNetworkAccessManager和HTTP持久连接

    HTTP 1 1 默认支持持久连接 因此我想使用发出第一个 HTTP 请求时设置的相同连接来发送第二个 http 请求 如何通过Qt来实现这一点 如果我只是在第一个请求完成后发出第二个请求 如下所示 manager gt get QNetw
  • 当维度在类实例化时未定义时,如何将浮点数组声明为 Objective-C 中的类变量?

    在 Java 中 它看起来像这样 class Foo float array Foo instance new Foo instance array new float 10 你可以只使用一个指针 float array Allocate
  • 列出Innodb全文索引的单词

    在 Mysql Innodb 中 我创建了一个全文索引 是否有一个查询可以用来检索全文索引中包含的所有单词的列表 我的想法是使用一个文本字段来保存标签列表的 json 转储 我可以使用全文查询来检索与标签匹配的行 它有效 剩下的问题是检索索
  • ctypes 结构中的默认值

    在 ctypes 结构中 是否可以指定默认值 例如 使用常规 python 函数 您可以执行以下操作 def func a b 2 print a b 这将允许这种行为 func 1 prints 3 func 1 20 prints 21
  • 从传统的基于 XUL 的附加组件中访问附加 SDK?

    我有一个大型且复杂的基于 XUL 的插件 我需要使用插件 SDK 中的一些函数 这可能吗 如果是这样 是否有人有工作示例代码 最好使用 page worker 模块 以下是 devtools 的做法 但某些模块会阻塞 明显的候选者是self
  • 无法访问viewBinding

    我现在正在 Kotlin 中使用 viewBinding here is my build gradle 启用 ViewBinding 并具有自动导入 ViewBinding 依赖项 but Android Studio still sho
  • 如何更好地组织数据库以适应用户状态的变化

    我关注的用户可以是 未确认 或 已确认 后者意味着他们获得完全访问权限 而前者意味着他们正在等待主持人的批准 我不确定如何设计数据库来解释这种结构 我的一个想法是拥有两个不同的表 confirmedUser 和 unconfirmedUse
  • 获取parent.location.url - iframe - 从子级到父级

    我得到了一个在 iframe 中显示的页面 我需要从该页面 子页面 获取带有js的parent location url 两个站点位于不同的域中 我想 警报 父 位置 url 但我收到这个错误 权限被拒绝http 父域 http paren
  • 通过 ASP.NET Identity 和 Autofac OWIN 集成进行授权

    在这个问题的底部添加了更新 我有一个 Web 应用程序 它使用 MVC5 和 WebAPI2 以及 Autofac for DI 该应用程序使用 ASP NET Identity 和 oAuth 不记名令牌 尽管后者可能不是重点 这一切都运
  • C#:存储百分比,50 还是 0.50?

    当在变量中保存百分比值时 会优先将它们保存为整数还是分数 也就是说 变量应该保存 0 到 100 之间的数字还是 0 00 到 1 00 之间的数字 在任何一种情况下 保存值的变量都是十进制类型 我正在交互的数据库恰好将它们存储为 0 到
  • Ionic 项目中的 GSAP

    如何将 GSAP 库导入到 Ionic 项目中 只是使用npm 安装 gsap当我通过导入时不起作用 import TweenMax TimelineMax from gsap 我使用打字稿 谢谢 你不需要打字 我在几个项目中使用过它 所有
  • 克服 Windows 用户对象句柄限制

    我正在寻找在构建重量级 Windows 界面时处理用户对象句柄限制的高级策略 请解释您如何使用 SWT 或直接 Windows GUI API 克服或绕过此问题 我唯一不感兴趣的是优化小部件使用的策略 因为我已经广泛地这样做了 但它并没有解
  • 为什么 RefCell:borrow_mut 在短路布尔 AND (&&) 两侧使用时会导致 BorrowMutError?

    我为 leetcode 编写了这段代码同一棵树问题 https leetcode com problems same tree use std cell RefCell use std rc Rc Definition for a bina
  • 读取文本文件 - fopen 与 ifstream

    谷歌搜索文件输入我发现了两种从文件输入文本的方法 fopen 和 ifstream 下面是两个片段 我有一个文本文件 其中包含一行 其中包含一个我需要读入的整数 我应该使用 fopen 还是 ifstream 片段 1 FOPEN FILE
  • meld - gi.glib.GError:主题中不存在图标“meld-change-apply-right”。安装有什么问题吗?

    我已经成功安装了 meld 3 14 2 和所有依赖包 通过从源代码编译每个包 并且所有包都安装在 NFS 共享上 prefix meld对于融合工具 prefix meld deps对于依赖项 最后 我调用了该工具 我可以看到 GUI 但
  • 隐藏水平滚动条

    我的水平滚动条有问题 我不想让它出现 实际上它只显示在 Chrome 中 而不会显示在 Internet Explorer 中 我能做些什么 我尝试过修改 css 类中的宽度和填充 但这也会改变布局 测试中的内容是动态的 因此它可以垂直溢出
  • 将 Java 类和方法移植到 Android。 (文本布局、字体、Graphics2D 等)

    我一直在 Android 中尝试并尝试通过 Java 应用程序进行移植 以下是我遇到的一些问题 希望得到一些指导 这是一个相当大的问题 而是多个问题 然而 我并不是盲目地询问他们 因为我已经对他们进行了研究 并试图运用我的理解 我花时间提出
  • 在 SQL Server 中将 COALESCE (或类似的东西)与 GROUP BY 一起使用

    我认为我缺少一些关于如何有效使用 GROUP BY 消除冗余记录的基本知识 我不断遇到似乎需要使用 COALESCE 的地方 但据我所知 这不适用于 GROUP BY 示例 我有一个表 其中包含访问 ID 和访问帐单代码的每种组合以及其他有
  • 使用 Cobertura 和 Jacoco 运行代码覆盖率

    我在获取 Maven 插件项目 使用调用程序插件进行集成测试 的 Sonar 中的集成测试和单元测试的代码覆盖率报告时遇到了一些问题 我无法使用默认的 Jacoco 覆盖率工具进行单元测试 因为这些工具使用 Powermock 这会导致使用