【问题标题】:Relationship between cobertura and surefirecobertura和surefire之间的关系
【发布时间】:2013-02-03 09:50:04
【问题描述】:

1句话中的问题:“Cobertura没有产生正确的代码覆盖率”

下面是我的 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>a.b.c</groupId>
    <artifactId>MyProject</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <repositories>
        <repository>
            <id>google-api-services</id>
            <url>http://google-api-client-libraries.appspot.com/mavenrepo</url>
        </repository>
    </repositories>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java-version>1.6</java-version>
        <maven.test.skip.exec>false</maven.test.skip.exec>
        <cobertura-maven-plugin.version>2.5.2</cobertura-maven-plugin.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>com.google.appengine</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>1.7.4</version>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <extensions>false</extensions>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.1.3</version>
                <configuration>
                    <testFailureIgnore>false</testFailureIgnore>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>${cobertura-maven-plugin.version}</version>
                <configuration>
                    <formats>
                        <format>xml</format>
                        <format>html</format>
                    </formats>
                    <check/>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>cobertura</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>${cobertura-maven-plugin.version}</version>
            </plugin>
        </plugins>
    </reporting>
    <dependencies>
       ...
    </dependencies>
</project>

当我尝试用这个 pom 构建时,会发生两件事

  1. 所有测试运行 2 次​​li>
  2. 战争爆发了
  3. Cobertura 报告显示覆盖率为 0%

请帮我调试这个问题。

【问题讨论】:

  • 您使用的确切的mvn 命令(和参数)是什么?
  • 我会试试mvn clean install cobertura:cobertura
  • 首先使用 mvn site...此外,如果测试无法使用 surefire 2.13 运行,那么您应该尽可能多地显示使用代码等。跨度>
  • 使用 jacoco,它是现代的并且仍然受支持,请参阅 my answer

标签: maven pom.xml cobertura maven-surefire-plugin


【解决方案1】:

我注意到的第一件事是您使用的是极旧版本的maven-surefire-plugin(2.1.3,大约是 2006 年!),但当前版本是 2.13。

除此之外,您将cobertura-maven-plugin 绑定到带有报告目标cobertura 的打包阶段,这是完全错误的。

最好先简化您的设置并让它运行,这意味着只需在属性中定义 cobertura-maven-plugin 的版本,然后在报告区域中进行设置,如下所示:

  <properties>
    <cobertura-maven-plugin.version>2.5.2</cobertura-maven-plugin.version>
  </properties>

并将以下内容放入报告区域:

<project>
  ..
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>${cobertura-maven-plugin.version}</version>
      </plugin>
      ...
    </plugins>
  </reporting>
</project>

只需使用此设置对其进行测试,然后检查是否已创建代码覆盖率。如果不是,您需要展示更多项目(pom、测试等)。

【讨论】:

  • 我按照你的指示得到了同样的结果。唯一的区别是在 2.13 中,测试根本不运行。使用 2.1.3 运行所有测试。很奇怪。根据您的建议,我将在上面发布整个 pom.xml。再次感谢您的调查。
  • 另请注意,如果我根本没有任何阶段,cobertura 根本不会运行
【解决方案2】:

首先,当您希望生成覆盖率报告时,我建议您运行 mvn clean install cobertura:cobertura。这不太可能是您想要为每个构建做的事情(我个人只在 Jenkins 中使用 Cobertura)。

其次,让所有测试运行两次似乎很烦人,但有些人认为这更可靠,因此是预期的行为。 这是因为 cobertura 会检测您的字节码。因此,这会影响您的测试结果的可能性(非常小)。

当然,由于这种双重测试运行很耗时,这也是您不会在标准生命周期中运行 cobertura:cobertura 的另一个原因

【讨论】:

    猜你喜欢
    • 2022-07-02
    • 1970-01-01
    • 2012-03-08
    • 2011-09-06
    • 2016-06-10
    • 2019-07-10
    • 2017-04-04
    • 2010-12-12
    • 1970-01-01
    相关资源
    最近更新 更多