6
votes

I am running Sonar Java code analysis on RHEL6 machine using Ant. For Integration test analysis I am using JaCoCo plugin for sonar. I have the plugin in my library class path. When I run my Selenium tests, a "jacoco.exec" file is generated (around 1MB for 10 tests). I then activate the Jacoco plugin in my Sonar Ant target and import it to Sonar. Sonar analysis logs say that Jacoco file has been analysed (took around 5000ms). However, My Sonar IT widget displays 0% code coverage. I have successfully got the unit test code coverage by using Cobertura.

My test target:

<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
        <classpath path="${buildHome}/libs/jacocoant.jar"/>
    </taskdef>

    <jacoco:coverage xmlns:jacoco="antlib:org.jacoco.ant">
        <junit fork="yes" failureproperty="true" forkmode="once" maxmemory="1024m">
            <formatter type="xml" />
            <classpath refid="buildClasspath" />

            <test name="${testName}" todir="${testLogs}" if="testcase" />
            <batchtest haltonerror="false" todir="${testLogs}">
                <fileset dir="${SeleniumScripts">
                    <include name="**/*.java" />
                </fileset>
            </batchtest>
        </junit>
    </jacoco:coverage>

My Sonar Ant target:

...
<property name="sonar.sources" value="${srcCode" />

    <property name="sonar.tests" value="${testCode}" />

    <property name="sonar.binaries" value="${srcAndTestBinaries}" />

    <property name="sonar.dynamicAnalysis" value="reuseReports"/>
    <property name="sonar.surefire.reportsPath" value="${reportsPath}" />
    <property name="sonar.core.codeCoveragePlugin" value="jacoco" />
    <property name="sonar.jacoco.itReportPath" value="${jacocoCoveragePath}/jacoco.exec" />

    <!-- Add the Sonar task -->
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <classpath path="${antLibPath}/sonar-ant-task-1.4.jar" />
    </taskdef>

    <sonar:sonar key="projectKey" version="1.0" xmlns:sonar="antlib:org.sonar.ant"/>

I have tried to include only "relevant" stuff. I am unsure if the "jacoco:coverage" element is corrent for Selenium tests, but in execution logs it mentions the "agent" so I assume the agent is used here as a proxy too on the Java VM.

I'm stuck, please help :)

Updated Here is part of the Sonar output:

[sonar:sonar] 08:36:15.619 INFO  p.PhasesTimeProfiler - Sensor JaCoCoItSensor...
[sonar:sonar] 08:36:15.623 INFO  o.s.p.j.JaCoCoPlugin - Analysing [file path omitted]\jacoco.exec
[sonar:sonar] 08:36:17.272 INFO  p.PhasesTimeProfiler - Sensor JaCoCoItSensor done:     1653 ms
2

2 Answers

0
votes

Hi I am also in the process of generating code coverage for acceptance test using jacoco. For unit test I am using cobertura and I am able to see the code coverage and unit test report on sonar dashboard.

Now coming to getting code covergae for acceptance test. We run acceptance test using selenium. I am wondering how does jacoco generates code coverage for acceptance test that is run using selenium.

From my guess I think you should mention path for jacocoagent.jar instead of jacocoant.jar. I could be wrong too. If you have found a way of generating code coverage for the test which runs on selenium could you provide the solution

0
votes

I may be missing it, but I don't see where you're telling the agent where to store the results.

In my ant script, I've got this:

 <jacoco:coverage enabled="${my.coverage.enabled}" destfile="${test.log.dir}/jacoco.exec">

wrapping the junit task. Try defining destfile="${jacocoCoveragePath}/jacoco.exec" and see if that fixes it.