5

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 次
  2. 战争产生
  3. Cobertura 报告显示覆盖率为 0%

请帮我调试这个问题。

4

2 回答 2

3

我注意到的第一件事是您使用的是非常旧的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、测试等)。

于 2013-02-18T21:28:03.160 回答
2

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

其次,让所有测试运行两次可能看起来很烦人,但有些人认为这是更可靠的,也是预期的行为。这是因为 cobertura 会检测您的字节码。因此,这与您的测试结果相混淆的可能性很小。

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

于 2013-02-21T15:54:27.340 回答