2

テスト結果の出力として sonarqube を使用し、テスト ケースのテストには maven と jacoco を使用しています。

Sonarqube のバージョンは 5.4 Maven のバージョンは 3.3.9 Jacoco のバージョンは 0.7

これは私のpom.xmlです

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.sonarqube</groupId>
  <artifactId>example-ut-maven-jacoco</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>Java :: UT Coverage with JaCoCo :: Maven</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <!-- Minimal supported version is 4.7 -->
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.6.201602180812</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
         <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <executions>
        <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the Surefire plugin is executed.
        -->
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <!--
            Ensures that the code coverage report for unit tests is created after
            unit tests have been run.
        -->
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
    </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.15</version>
        <configuration>
            <!-- Sets the VM argument line used when unit tests are run. -->
            <argLine>${surefireArgLine}</argLine>
            <!-- Skips unit tests if the value of skip.unit.tests property is true -->
            <skipTests>${skip.unit.tests}</skipTests> 
        </configuration>
    </plugin>
    </plugins>
  </build>



</project>

こちらのリンクもたどっていますが、ここで使用しているファイルについては、ここにリンクの説明を入力してください。

これは、テスト用のプロジェクトを構築する方法です

mvn clean test
mvn clean verify
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install
mvn sonar:sonar

また、sonarqube localhost:9000/ に移動してこれを確認すると、0.00% のコード カバレッジが表示されますが、単体テストはすべて成功として表示され、「成功しました」と表示されます。

この問題の原因について何か考えはありますか? また、私はまだこれらすべてに慣れていません。

4

1 に答える 1

2

私の観点からは、セットアップは問題ないように見えます。

レポート ファイルのデフォルト名を変更しました。したがって、ソナーアナライザーにもそれを伝える必要があると思います。

単体テストのjacocoのデフォルトは次のとおりです。これは${project.build.directory}/jacoco.exec 、ソナーが取得しようとする名前でもあります。

ソナー アナライザーの場所を示すプロパティを設定するだけです。

<properties
    <sonar.jacoco.reportPath>${project.build.directory}/coverage-reports/jacoco-ut.exec</sonar.jacoco.reportPath>
    ...

jacco ファイルが存在し、空でないことを確認してください。その場合、場所が正しく設定されると、ソナーは遅かれ早かれそれを取得します。

于 2016-03-24T12:51:20.497 に答える