【问题标题】:Maven code coverageMaven 代码覆盖率
【发布时间】:2014-01-14 08:56:29
【问题描述】:

我是 Java 世界的新手。我们的团队正在使用 Maven 将所有内容构建到单个 .war 文件中。我正在寻找工具来检测 .war 文件以启用代码覆盖率。想法是手动检测 .war 文件,然后运行测试。

我查看了几个工具,但没有得到我正在寻找的东西,例如Emma、jester、cobertura 等。寻找简单的说明。

【问题讨论】:

  • Maven 是非常好的构建工具。寻找新工具有什么具体原因吗?..
  • 我想知道如何使用 Maven 检测它以进行代码覆盖构建。
  • 仪器.war文件???通常你会在你的项目上运行 maven 测试,如果 100% 通过然后继续 mvn 包,这将构建你的 war 文件。代码覆盖率符合 Maven 测试步骤。您可以存储 html 报告,整合统计信息,并且仅在行覆盖率 > 90% 时才进行打包步骤。我在我的 Grails 项目中使用 cobertura,在我的 Java/groovy 项目中使用 Emma。安装/使用非常简单。不要重新发明轮子!!!

标签: java maven instrumentation war


【解决方案1】:

如果您想测量代码覆盖率,您应该使用 Jacoco。它还允许测量单元测试和集成测试。

你所要做的就是添加依赖:

  <dependency>
    <groupid>org.jacoco</groupid>
    <artifactid>org.jacoco.core</artifactid>
    <version>0.6.2.201302030002</version>
    <scope>test</scope>
  </dependency>

并添加 jacoco-maven-plugin。请注意,如果您不使用 Sonar,则必须将 ${sonar.jacoco.reportPath} 属性替换为原始文件路径

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.6.2.201302030002</version>
  <executions>
    <!-- prepare agent for measuring unit tests -->
    <execution>
      <id>prepare-unit-tests</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <configuration>
        <destFile>${sonar.jacoco.reportPath}</destFile>
      </configuration>
    </execution>

    <!-- prepare agent for measuring integration tests -->
    <execution>
      <id>prepare-integration-tests</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <phase>pre-integration-test</phase>
      <configuration>
        <destFile>${sonar.jacoco.itReportPath}</destFile>
        <propertyName>itCoverageAgent</propertyName>
      </configuration>
    </execution>
  </executions>
</plugin>

如果您还想使用声纳,请指定此类属性:

<properties>
  <!-- select JaCoCo as a coverage tool -->
  <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
  <!-- force sonar to reuse reports generated during build cycle -->
  <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
  <!-- set path for unit tests reports -->
  <sonar.jacoco.reportPath>${project.basedir}/target/jacoco-unit.exec</sonar.jacoco.reportPath>
  <!-- all modules have to use the same integration tests report file -->
  <sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
</properties>

您可以在http://www.kubrynski.com/2013/03/measuring-overall-code-coverage-in.html找到更多详细信息

【讨论】:

    【解决方案2】:

    Cobertura 会支持这一点。请参阅此问题的答案。

    Java: measure code coverage for remote scripting tests

    如果您想在开发中而不是在构建服务器上执行此操作,您可能想尝试一下 eclemma。您可以使用 eclemma 在 IDE 中启动您的 webapp,然后简单地运行您想要运行的任何测试(在 eclemma 之外),它会很好地注释以绿色运行的代码。

    【讨论】:

    • 我正在尝试使用 cobertura-2.0.3 检测 .war 文件。当我运行 cobertura-instrument.bat 时。我在线程“main”Java.lang.NoClassDefFoundError 中遇到异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 2021-07-21
    • 2016-10-29
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    相关资源
    最近更新 更多