【问题标题】:cobertura-maven-plugin excludes configurationcobertura-maven-plugin 不包括配置
【发布时间】:2013-02-20 12:14:29
【问题描述】:

我有一个 Maven 项目,在目录 /src/test/java/test/com/mycompany/myproduct/android/viewtype2fragmentmapper/ 中有一个测试用例 DefaultViewTypeToFragmentMapperTest.java

我希望将此测试用例排除在单元测试覆盖率计算之外。为了达到这个效果,我这样配置插件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.5.2</version>
    <configuration>
        <formats>
            <format>html</format>
            <format>xml</format>
        </formats>
        <instrumentation>
            <excludes>
                <exclude>test/co/**/*.class</exclude>
            </excludes>
        </instrumentation>
    </configuration>
</plugin>

但我仍然在覆盖率报告中看到上述课程。

我怎样才能修复它以使测试用例不出现在报告中并且其覆盖率(根据报告为 0 %)不被考虑在内?

【问题讨论】:

  • 您是否将 cobertura-maven-plugin 配置为报告或构建的一部分?如果您配置为报告,请使用mvn site。如果您想执行mvn cobertura:cobertura,请配置为构建。请在stackoverflow.com/questions/15133822/…查看我的回答以获取更多信息。
  • 我将其配置为&lt;reporting&gt; 节点的一部分,并且仅在mvn site 范围内使用。
  • 我发现该插件有一个有用的调试日志。使用mvn cobertura:... -X 启用它

标签: java maven maven-3 cobertura


【解决方案1】:

经过多次尝试并失败后,我得到了它的工作。

  1. 编辑 pom。
  2. mvn clean test-compile
  3. mvn cobertura:cobertura
  4. 从 Firefox 重新打开页面。 (确保页面没有被缓存)

我得到了它的工作:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.6</version>
        <configuration>
            <instrumentation>
                <excludes>
                    <exclude>aaa/**/*.class</exclude>
                    <exclude>com/test/bbb/**/*.class</exclude>
                </excludes>
            </instrumentation>
        </configuration>
 </plugin>

将“aaa”更改为要排除的包名称的开头。 将“bbb”更改为您要从报告中排除的包名称。

希望对你有帮助 马克·安德鲁

【讨论】:

    【解决方案2】:

    你应该使用&lt;ignore&gt;标签。

    <configuration>
      <instrumentation>
        <ignores>
          <ignore>com.example.boringcode.*</ignore>
        </ignores>
      </instrumentation>
    </configuration>
    

    &lt;instrumentation&gt; 中使用的&lt;exclude&gt; 只是将包从您的检测中排除。在这种情况下,这没什么,因为你什么都没做。

    请查看Mojo Maven Cobertura Plugin docs here

    【讨论】:

    • 好的,让我们回到您的排除语句。 test/co/**/*.class 对你来说是什么?您尝试排除的测试不在此包结构中。
    • 文档并没有真正解释 excludeignore 之间的区别。
    【解决方案3】:

    是错字吗?

    &lt;exclude&gt;test/co/**/*.class&lt;/exclude&gt;

    co 应该是 com

    顺便说一句,&lt;ignores&gt; 指示 Cobertura 忽略对与忽略正则表达式匹配的任何方法的任何调用。它会在检测期间跳过这些类。要排除检测类,应使用&lt;excludes&gt;

    【讨论】:

    • 不,不是。 The test.co.mycompany.* 包名正确。
    • 您的文件夹名称与该软件包名称不匹配。你需要把“com”你的帖子说代码在“/src/test/java/test/com/mycompany/myproduct/android”中
    【解决方案4】:

    你不应该像下面的例子那样附加 .class

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.5.2</version>
        <configuration>
            <formats>
                <format>html</format>
                <format>xml</format>
            </formats>
            <instrumentation>
                <excludes>
                    <exclude>test/co/**/*</exclude>
                </excludes>
            </instrumentation>
        </configuration>
    </plugin>
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案5】:

      我刚刚失去了两个小时的生命,获得了排除 Cobertura 的排除,但最终成功了。

      我找到的解决方案是,带有检测和排除 cobertura-maven-plugin 的插件配置必须在 pom 的 build/plugins 或 build/pluginManagement 章节中,而 必须在报告章节中引用 cobertura-maven-plugin。

      这里的陷阱是您最初从在报告章节定义插件开始,否则不会生成报告。但是仪器本身并没有从 pom 的那部分获取任何配置。您需要在构建章节中定义它。

      <build>
          <pluginManagement>
              <plugins>
                  <plugin>
                      <groupId>org.codehaus.mojo</groupId>
                      <artifactId>cobertura-maven-plugin</artifactId>
                      <version>2.7</version>
                      <configuration>
                          <instrumentation>
                              <excludes>
                                  <exclude>my/exclusion/package/**</exclude>
                              </excludes>
                          </instrumentation>
                      </configuration>
                  </plugin>
              </plugins>
          </pluginManagement>
      </build>
      
      <reporting>
          <plugins>
              <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>cobertura-maven-plugin</artifactId>
              </plugin>
          </plugins>
      </reporting>
      

      【讨论】:

        【解决方案6】:

        除了BertKoor 的回答,我想指出的是,如果您直接执行mvn cobertura:coberturamvn cobertura:cobertura-integration-test,您的报告将仍然涵盖所有在您的目标目录中找到检测类,如报告here

        如果是这种情况,请确保您执行mvn **clean** cobertura:cobertura,以便首先从以前的构建中清理目标目录,然后检测并运行您的测试。

        【讨论】:

          猜你喜欢
          • 2012-01-07
          • 2012-01-16
          • 1970-01-01
          • 2012-02-12
          • 1970-01-01
          • 2015-06-18
          • 2011-03-13
          • 2014-10-26
          • 1970-01-01
          相关资源
          最近更新 更多