Search code examples
antjunitsonarqubeemma

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?


Solution

  • The property 'sonar.surefire.reportsPath' needs to be defined before the definition of the sonar target.

    The following definition gets the test info exported (although it's still not exporting coverage info):

    <property name='sonar.surefire.reportsPath' value='${test.dir}'/>
    
    <property name='sonar.dynamicAnalysis' value='reuseReports'/>
    <property name='sonar.emma.reportPath' value='${coverage.report.dir}'/>
    
    <target name='sonar'>
        <property name='sonar.sources' value='${src.dir}'/>
        <property name='sonar.tests' value='${test.src.dir}'/>
        <property name='sonar.binaries' value='${build.dir}'/>
        <path id='jars'>
            <fileset dir='${env.JAVA_HOME}/jre/lib' includes='*.jar'/>
            <fileset dir='${ivy.lib.dir}/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'/>
    </target>