Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

linux - SONAR - Measure Code Coverage using Cobertura

I am using sonar to measure code quality. One thing that I do not know is the steps to measure code coverage using Cobertura.

I followed the steps from http://cobertura.sourceforge.net/anttaskreference.html and was able to generate xml files. How do I get these xml files into SONAR?

Is there an easier way to use Cobertura in SONAR?

I am running the code coverage (Cobertura) in a different server than my SONAR server. Both servers are running under LINUX.

Thanks for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You configure the Sonar task to upload unit test and cobertura reports generated by other parts of your build logic.

This is in contrast to Maven which has a standard build life-cycle that Sonar is able to leverage.

Unit test and code coverage

The following logic runs the unit tests with cobertura instrumented classes. An XML coverage report is generated by cobertura at the end:

<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>

Invoking Sonar

I normally use a very simple Sonar 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>

And use a properties file to control all aspects of Sonar's behaviour:

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

Demonstrates how Sonar can be configured to pick up the unit test reports created by junit and the code coverage report generated by cobertura.

The build does not have to run on the same server as Sonar. In that case one must provide the remote Sonar URL and JDBC credentials.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...