Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Sonar to export test stats?

I have the following Sonar Ant target defined:

<target name='sonar'>
    <property name='sonar.sources' value='${src.dir}'/>
    <property name='sonar.tests' value='${test.src.dir}'/>
    <property name='sonar.binaries' value='build/classes'/>
    <path id='jars'>
        <fileset dir='${env.JAVA_HOME}/jre/lib' includes='*.jar'/>
        <fileset dir='build/lib/test' includes='*.jar'/>
    </path>
    <pathconvert property='sonar.libraries' refid='jars' pathsep=','/>

    <exec executable='p4' outputproperty='p4.P4CLIENT'>
        <arg value='set'/>
        <arg value='P4CLIENT'/>
    </exec>
    <propertyregex
            property='p4client'
            input='${p4.P4CLIENT}'
            regexp='P4CLIENT=([^ ]+) *.*'
            replace='\1'/>
    <propertyregex
            property='sonar.timestamp'
            input='${build.time}'
            regexp='_'
            replace='T'/>
    <sonar:sonar key='com.netflix:${module.name}' version='${p4client}@${sonar.timestamp}' xmlns:sonar='antlib:org.sonar.ant'/>

    <property name='sonar.dynamicAnalysis' value='reuseReports'/>
    <property name='sonar.emma.reportPath' value='${coverage.dir}'/>
</target>

When I run 'ant sonar' and bring up Sonar in my browser, I see info about the classes in the src directory, but nothing about the stuff in the test directory.

If I add ${test.src.dir} to sonar.sources and not set sonar.tests, I see some info about the test classes, but Sonar still reports 0 Test Successes.

How do I get it so I can drill down to each test method and their stats?

like image 979
Noel Yap Avatar asked Aug 27 '11 06:08

Noel Yap


1 Answers

For anyone else that runs across this issue, I finally got Sonar to report on our Emma Code coverage. The first problem was that the Emma plugin did not come with the version of Sonar I was using (3.1.1). I had to download it and install it to the extensions/plugins directory of Sonar and restart it.

Then I had to set the following properties in my build.xml:

<property name="sonar.core.codeCoveragePlugin" value="emma" />
<property name="sonar.emma.reportPath" value="${coverage.dir}" />

After this, I atleast saw the following output after running the Sonar ant task:

[sonar:sonar] 13:41:49.705 WARN        org.sonar.INFO - No coverage (*.ec) file found in /my/local/path
[sonar:sonar] 13:41:49.708 WARN        org.sonar.INFO - No metadata (*.em) file found in /my/local/path

After some digging, I found that inside of the Sonar Emma plugin, it is hard-coded to look for a .ec (coverage) file and a .em (metadata) file. Unfortunately, my coverage file had a .emma extension as did my metadata file and I was unable to rename them as it would break other functionality. So I wrote the following Ant task to copy the files to match the naming standard that the Sonar Emma plugin expects.

<target name="createEmmaFilesWithSonarNamingStandard" depends="defineAntContribTasks">
    <if>
        <available file="${coverage.dir}/metadata.emma" />
        <then>
            <copyfile src="${coverage.dir}/metadata.emma" dest="${coverage.dir}/metadata.em" />
        </then>
    </if>
    <if>
        <available file="${coverage.dir}/coverage.emma" />
        <then>
            <copyfile src="${coverage.dir}/coverage.emma" dest="${coverage.dir}/coverage.ec" />
        </then>
    </if>
</target>

After running this again, I came across a new problem:

org.sonar.api.utils.SonarException: java.io.IOException: cannot read [/my/local/path/build/coverage/metadata.em]: created by another EMMA version [2.0.5312]

After some more digging, I found that the Sonar Emma 1.0.1 plugin was compiled against Emma 2.0.5312 and the Sonar Emma 1.1 and 1.2.x against Emma version 2.1.5320 as stated on the Sonar Emma plugin page.

I downloaded the 2.1.5320 version of Emma, replaced both emma.jar as well as emma_ant.jar in my Ant lib directory. After a clean re-compile and test, I was able to re-run the Sonar Ant task and have my code coverage reflected on Sonar.

like image 166
frankjl Avatar answered Oct 01 '22 22:10

frankjl