2016-03-24 73 views
1

SonarQube爲我的項目報告了以下內容。SonarQube無法看到Junit cobertura覆蓋率報告

Unit Tests Coverage 0.0%; Line Coverage 0.0%; Condition Coverage 0.0% 

它無法找到Sonar掃描前立即創建的報告。我正在使用Jenkins來啓動SonarQube掃描。事實上,在控制檯輸出中,我可以看到正在執行的單元測試以及保存在surefire目錄中的報告。

下面是控制檯輸出的相關日誌。

[INFO] --- maven-surefire-plugin:2.12:test (default-test) @ earn-promotional-domain --- 
[INFO] Surefire report directory: /var/jenkins/workspace/earn/surefire-reports 

[INFO] [13:50:20.807] Sensor SurefireSensor 
[INFO] [13:50:20.807] parsing /var/jenkins/workspace/earn/surefire-reports 
[ERROR] [13:50:20.808] Reports path not found or is not a directory: /var/jenkins/workspace/earn/surefire-reports 
[INFO] [13:50:20.808] Sensor SurefireSensor (done) | time=1ms 
[INFO] [13:50:20.808] Sensor CoberturaSensor 
[INFO] [13:50:20.808] parsing /var/jenkins/workspace/earn/site/cobertura/coverage.xml 

我正在使用SonarQube 5.1.2。請讓我知道是否需要任何其他信息,這將有助於找出問題。

+0

我使用JaCoCo,但過程應該是相同的(ish)。您需要注入儀器類的代理(http://www.mojohaus.org/cobertura-maven-plugin/)然後您需要觸發sonarQube分析。用cobertura(重用報告)的結果或再次運行測試。在這裏,我通過我的jenkins傳遞給JaCoCo的參數-Dsonar.binaries = target/classes -Dsonar.junit.reportsPath = target/surefire-reports -Dsonar.jacoco.reportPath = target/coverage-reports/jacoco-ut.exec – drgn

+0

謝謝@drgn!我不知道我需要給目標目錄作爲參數,因爲我有一個不同的項目,我不必給目標目錄。我能看到的唯一區別是,在不工作的項目中,有很多子項目,並且由於有很多子目錄。你認爲我必須將每個文件夾作爲聲納報告路徑嗎? –

+0

這些子項目是否擁有自己的pom.xml文件? – drgn

回答

1

你最好用Jacoco比cobertura。 Cobertura有許多尚未解決的開放漏洞。

Jacoco還提供了一個聚合器插件,它將聚合所有子模塊的覆蓋範圍並顯示全面的覆蓋範圍。

Jacoco也不需要額外的SonarQube插件。

這裏的實施Jacoco的一個很好的例子: http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/

+0

謝謝傑爾!我也傾向於這種選擇。我會嘗試雅科果,看看我是否有更好的運氣! –

0

如果你喜歡用的Cobertura堅持,而不是轉移到Jacoco,你可以試試這個替代的Cobertura Maven插件:

https://github.com/QualInsight/qualinsight-mojo-cobertura

這裏有一些優勢,這mojo提供:

  • 它運行測試一次而不是用的Cobertura,Maven的插件)
  • 它可以讓你分裂覆蓋(UT/IT /綜合)兩次
  • 你得到SonarQube

的文檔上擺脫「過時」的Cobertura插件該項目的頁面解釋瞭如何將轉換後的報告添加到SonarQube。

0

由於@Jeel表示,您可以使用替代插件。但是如果你一定要使用cobertura插件,你可以稍微修改一下。如果我理解正確,cobertura:check目標生成一個當前生命週期,合併一個特定於插件的配置(cobertura-maven-plugin-XXjar \ META-INF \ maven \ lifecycle.xml),並執行分叉生命週期測試階段。之後,「檢查」mojo被執行。

爲了使不的Cobertura到餐桌的生命週期,你可以發表評論<在檢查目標的一個插件描述符(的Cobertura-Maven的插件,X.X.jar \ META-INF \行家\ plugin.xml中)executePhase >標籤。 另外,您必須將配置從lifecycle.xml複製到您的構建配置。對於2.7版本,只有神火配置:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>${surefire.version}</version> 
    <configuration> 
    <classesDirectory>${project.build.directory}/generated-classes/cobertura</classesDirectory> 
    <testFailureIgnore>false</testFailureIgnore> 
    </configuration> 
</plugin> 

(有可能延長使用components.xml也將工作一個默認的生命週期)。

在最後一步中,您必須在「process-class」階段爲「instrument」目標添加執行,因爲它在插件描述符中沒有缺省階段。

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>cobertura-maven-plugin</artifactId> 
    <version>${cobertura.version}</version> 
    <executions> 
    <execution> 
     <id>instrument-classes</id> 
     <phase>process-classes</phase> 
     <goals> 
     <goal>instrument</goal> 
     </goals> 
    </execution> 
    <execution> 
     <id>check-coverage</id> 
     <goals> 
     <goal>clean</goal> 
     <goal>check</goal> 
     </goals> 
    </execution> 
    </executions> 
</plugin> 
相關問題