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でビルドしようとすると、2つのことが起こります

  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 のみを使用しています)。

第 2 に、すべてのテストを 2 回実行するのは面倒に思えるかもしれませんが、これはより信頼性が高く、意図した動作であると考える人もいます。これは、cobertura がバイトコードを計測するためです。したがって、これがテストの結果を台無しにする (ごくわずかな) 可能性があります。

しかしもちろん、この二重のテスト実行には時間がかかります。これが、標準的なライフサイクルで cobertura:cobertura を実行しないもう 1 つの理由です。

于 2013-02-21T15:54:27.340 に答える