SonarQube Coverage per Test Information with ANT

Hi all,
I read a post on a similar topic in this community but that did not help me fix the issue I am facing.
(No information about coverage by test)
I replied on that post but had no response, which is why I am creating a new topic.
Basically, I am able to get all the coverage information on SonarQube except Coverage per Test which I have heard, is deprecated.
I read another thread on this forum which was dated this year and the person could successfully get the information on what tests covered particular line of code by clicking on the green bar on the side when on the sonarqube dashboard. (SonarQube code coverage by JaCoCo)

I would really like this information in my sonarqube project.

I am using the following:
sonarqube-6.7.6
sonar-scanner-cli-3.2.0.1227.jar
jacocoant.jar from jacoco v0.8.2

The unit tests are run as part of an Ant build.
I have wrapped the junit tests with jacoco coverage tags:

<jacoco:coverage enabled="{coverage.enabled}" destfile="{jacoco.dir}/steps_jacoco.report">

  <junit maxmemory="1024M"
                   fork="true"
                   forkmode="perBatch"
                   haltonfailure="false"
                   failureproperty="tests.failed"
        		   printsummary="withOutAndErr"
                   showoutput="true">

                <formatter type="xml" />

                <classpath>
                    <path refid="additional-junit-classpath" />
                    <fileset refid="fileset:third-party_jars-for-tests" />
                    <pathelement path="${project.build.dir}/acceptanceTest-classes" />
                    <pathelement path="${project.build.dir}/test-classes" />
                    <pathelement path="${project.build.dir}/../core/test-classes" />
                </classpath>
                <batchtest todir="${test.result.dir}">
                    <fileset dir="${project.build.dir}/../core/acceptanceTest-classes">
                        <include name="**/blah.class" />
                        <include name="**/blah.class" />
                    </fileset>
                    <fileset dir="${project.build.dir}/acceptanceTest-classes">
                        <include name="**/*Test.class" />
                        <exclude name="**/blah.class" />
                    </fileset>
                </batchtest>
   </junit>

</jacoco:coverage>

The jacoco report is successfully created after running the Ant build with the above code.
After this I run the sonar-scanner on a linux environment using the following command line command:

sonar-scanner -Dproject.settings=$locationOfPropertyFile

image

Some properties from the sonarqube.properties file:

sonar.junit.reportPaths={env.blah}/test-results sonar.java.coveragePlugin=jacoco sonar.surefire.reportsPath={env.blah}/test-results
sonar.jacoco.reportPaths=${env.blah}/jacoco_steps_report/steps_jacoco.report

Is there anyway I can get the coverage per test information?
Your help with be highly appreciated :slight_smile:

Regards,
Dhawal

Hi Dhawal,

No. We dropped that quite a while ago.

 
Ann

Hi Ann,

Thanks for your reply :slight_smile:
Out of curiosity, any reason why a maven example I got from a website displays the coverage per test information when run?
I have read that it has to do with jacoco junit listener. Can’t this be achieved with an ANT build running the unit tests?

The example I am talking about is here:

Regards,
Dhawal

Hi Dhawal,

It’s possible I’m mis-remembering how long ago that was dropped. Could you share a screenshot?

 
Ann

Hi Ann,

Sure :slight_smile:
I have attached the screenshot of the coverage tab with the coverage per test feature working.

It uses surefire junit listener for the Coverage per test scan. This is what is used in the pom.xml:

<profiles>
  <profile>
    <id>coverage-per-test</id>
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <!-- Minimal supported version is 2.4 -->
          <version>2.13</version>
          <configuration>
            <properties>
              <property>
                <name>listener</name>
                <value>org.sonar.java.jacoco.JUnitListener</value>
              </property>
            </properties>
          </configuration>
        </plugin>
      </plugins>
    </build>

    <dependencies>
      <dependency>
        <groupId>org.sonarsource.java</groupId>
        <artifactId>sonar-jacoco-listeners</artifactId>
        <version>3.8</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </profile>
</profiles>

I tried finding a way to use it in ANT but was unsuccessful.
Would you know about it?

Thanks,
Dhawal

Hi Dhawal,

My experience with the Ant scanner is that it’s far easier to just <exec> the CLI scanner. That said, this is about passing the report location via the right property and theoretically, you should be able to just create an Ant property with the correct name and it will be passed forward.

 
Ann

Hi Ann,
As mentioned in a previous comment, I have the following paths (and more) set up in the sonarscanner properties file. I can see the unit tests and coverage information on the SonarQube dashboard but I am only missing the coverage per test information.
I suspect I am missing something like a junit test listener when performing jacoco instrumentation.
In ANT, I thought tags would do the job but it didn’t work.
Is there a way I can run the unit tests with ANT and have jacoco.JunitListener attached to it?

Thanks,
Dhawal

Hi Dhawal,

Sorry, but my Ant experience never stretched to that.

 
Ann

Hi Ann,

It’s okay. I guess we’ll have to use SonarQube without the Coverage per Test feature :frowning:

Thanks for your effort to help me with this issue. Have a nice day :slight_smile:

Cheers,
Dhawal