2011-10-12 13 views
9

私はコード品質を測定するためにソナーを使用しています。私が知らないことの1つは、Coberturaを使用してコードカバレッジを測定するステップです。SONAR - Coberturaを使用してコードカバレッジを測定する

私はhttp://cobertura.sourceforge.net/anttaskreference.htmlの手順に従って、xmlファイルを生成することができました。これらのxmlファイルをSONARにどのように取得するのですか?

SONARにCoberturaを使用する簡単な方法はありますか?

コードカバレッジ(Cobertura)を、私のSONARサーバーとは異なるサーバーで実行しています。どちらのサーバもLINUXで動作しています。

ありがとうございました!

+0

このトピックの更新:サンプルはソナーサンプルリポジトリ=> https://github.com/SonarSource/sonar-examples/tree/master/projects/code-coverageにあります。 –

答えて

11

あなたは、ビルド・ロジックの他の部分によって生成されたユニットテストとCoberturaのレポートをアップロードするソナータスクを設定。

これは、Sonarが活用できる標準のビルドライフサイクルを持つMavenとは対照的です。

ユニットテストとコードカバレッジ

は、次のロジックがCoberturaのインストルメントクラスとユニットテストを実行します。XMLカバレッジレポートは、最後にCoberturaのによって生成されます

<target name="instrument-classes" depends="compile-tests"> 
    <taskdef resource="tasks.properties" classpathref="test.path"/> 
    <cobertura-instrument todir="${instrumented.classes.dir}" datafile="${build.dir}/cobertura.ser"> 
     <fileset dir="${classes.dir}"/> 
    </cobertura-instrument> 
</target> 

<target name="junit" depends="instrument-classes"> 
    <junit printsummary="yes" haltonfailure="yes"> 
     <classpath> 
      <path refid="test.path"/> 
      <pathelement path="${instrumented.classes.dir}"/> 
      <pathelement path="${test.classes.dir}"/> 
     </classpath> 

     <formatter type="xml"/> 

     <batchtest fork="yes" todir="${test.reports.dir}"> 
      <fileset dir="${test.src.dir}"> 
       <include name="**/*Test*.java"/> 
       <exclude name="**/AllTests.java"/> 
      </fileset> 
     </batchtest> 
    </junit> 
</target> 

<target name="test" depends="junit"> 
    <cobertura-report format="xml" datafile="${build.dir}/cobertura.ser" destdir="${cobertura.reports.dir}"/> 
</target> 

呼び出すソナー

私は通常、非常に単純なソナーターゲット使用

:すべての側面を制御するために、ファイル

<target name="sonar" depends="test"> 
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpathref="sonar.path"/> 

    <sonar:sonar key="${sonar.project.key}" version="${sonar.project.version}" xmlns:sonar="antlib:org.sonar.ant"/> 
</target> 

をして、プロパティを使用しますSonarの動作の例:

sonar.project.key=org.demo:demo 
sonar.project.version=1.0-SNAPSHOT 
sonar.projectName=Demo project 

sonar.host.url=http://myserver:9000 
sonar.jdbc.url=jdbc:mysql://myserver:3306/sonar?useUnicode=true&characterEncoding=utf8 
sonar.jdbc.driverClassName=com.mysql.jdbc.Driver 
sonar.jdbc.username=sonar 
sonar.jdbc.password=sonar 

sonar.sources=${src.dir} 
sonar.tests=${test.src.dir} 
sonar.binaries=${classes.dir} 

sonar.dynamicAnalysis=reuseReports 
sonar.surefire.reportsPath=${test.reports.dir} 
sonar.java.coveragePlugin=cobertura 
sonar.cobertura.reportsPath=${cobertura.reports.dir}/coverage.xml 

junitで作成された単体テストレポートとcoberturaで生成されたコードカバレッジレポートを取得します。

ビルドは、Sonarと同じサーバーで実行する必要はありません。その場合、リモートのSonar URLとJDBC認証情報を提供する必要があります。

+0

私はあなたの指示に従おうとしました。しかし、私は以前と同じ問題を抱えていますが、SONAR内のカバレッジ統計を見ることはできません。生成されたxmlファイルとserファイルは、以前のものと似ています。 SONARの統計で統計を取得できますか? – lwijono

+0

Sonarタスクの出力は、正しいカバレッジファイルをロードしていることを示していますか? –

+0

どうすれば確認できますか? – lwijono

2

あなたはソナーのpom.xmlにこれらのプロパティを追加する必要があります:(ご使用の環境に適切なパスで)

<properties> 
    <sonar.dynamicAnalysis>false</sonar.dynamicAnalysis> 
    <sonar.phase>generate-sources</sonar.phase> 
    <sonar.surefire.reportsPath>target/reports/test/</sonar.surefire.reportsPath> 
    <sonar.cobertura.reportPath>../project/target/reports/coverage/coverage.xml</sonar.cobertura.reportPath> 
</properties> 

をし、実行:

mvn sonar:sonar 

は詳細についてuser listをチェック。

+0

私のプロジェクトがMavenを使用しますか?それでも実行する必要がありますか? Coberturaのウェブサイトからは、ANTも使用できるようです。私が間違っている? 今、私はソーナーメイヴェン - コベルトゥラの関係と混同しています。たとえば、ビルドスクリプトを実行するantを呼び出してhudsonからビルドを呼び出すとします。ビルドスクリプトはビルドを行い、SONAR DBも更新します。さらに、もともと、私はCobertureを実行するビルドスクリプトにいくつかのコマンドを追加する必要があると思っていました。今私はよくわからない。私の場合、Cobertureをmavenでどのように呼び出すべきですか?他の解決策も歓迎されていますか?ありがとうございました。 – lwijono

+0

Sonarは最近までMavenに結合されました。 2.6(2011年3月)以降、ソナーはANTにも対応しています。上記のプロパティをantスクリプトに[here](http://docs.codehaus.org/display/SONAR/Analyse+with+Ant+Task+1.0)のように追加することができます。たとえば、次のようにします。 ' '私はSonar + Cobertura + Antの走者を走らせていませんが、それは違うはずはありません。また、Sonar Stand Alone(ANT、Mavenなどなし)[runner](http://docs.codehaus.org/display/SONAR/Analyse+with+a+simple+Java+Runner)をチェックアウトすることもできます – tolitius

1

Mavenを使用している場合は、POMファイルに特別な指定はありません。 "mvn clean sonar:sonar"を実行するだけで、Sonarは自動的にコードをコンパイルし、Cobertura(Sonarのデフォルトカバレッジエンジン)でテストを実行し、DB内のすべての結果をプッシュします。

MavenではなくAnt [1]または単純なJavaランナー[2]を使用している場合と同じです。

これまでSonvertを実行していたCobertura(Antタスクなど)を手動で実行する必要がないという主張があります。

[1] http://docs.codehaus.org/display/SONAR/Analyzing+with+Sonar+Ant+Task

を[2] http://docs.codehaus.org/display/SONAR/Analyse+with+a+simple+Java+Runner

ファブリス、 SonarSource

関連する問題