Code Coverage showing

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    Sonarqube - 10.0 (build 68432) Developer Edition
    Scanner - 4.8.0.2856
    Plugin - None
  • how is SonarQube deployed: zip, Docker, Helm
    Deployment - Helm chart
  • what are you trying to achieve
    Code coverage from sonar analysis (during MR and on default branch) from jacoco xml reports.
  • what have you tried so far to achieve this
    We’ve tried following all the documentations. Sonarqube is reading the jacoco xml file, but shows code coverage on the project.

Under the project in sonarqube → Overall Code it shows 0.0% coverage however at the same time it shows that there are 63 unit tests.

Current setup :

  • Jacoco - 0.8.10
  • Maven-surefire-plugin - 2.19.1
  • junit-platform-surefire-provider - 1.0.1
  • Properties for sonar
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <sonar.qualitygate.wait>true</sonar.qualitygate.wait>
        <sonar.projectKey>channels_channels-groups_AYeUjL6yHFmsriGWf4Nb</sonar.projectKey>
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
        <sonar.language>java</sonar.language>
    </properties>

Jacoco setup

<build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.8.10</version>
                        <executions>
                            <execution>
                                <id>prepare-agent</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>report</id>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.sonarsource.scanner.maven</groupId>
                        <artifactId>sonar-maven-plugin</artifactId>
                        <version>3.5.0.1254</version>
                    </plugin>
                </plugins>
            </build>

Maven surefire plugin

<plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.1</version>
                    </dependency>
                </dependencies>
            </plugin>

Command to execute the tests + analysis

mvn verify sonar:sonar

Log message from job in gitlab

[INFO] ------------- Run sensors on project
[INFO] Sensor Analysis Warnings import [csharp]
[INFO] Sensor Analysis Warnings import [csharp] (done) | time=1ms
[INFO] Sensor Zero Coverage Sensor
[INFO] Sensor Zero Coverage Sensor (done) | time=2ms
[INFO] Sensor Java CPD Block Indexer
[INFO] Sensor Java CPD Block Indexer (done) | time=76ms
[INFO] SCM Publisher SCM provider for this project is: git
[INFO] SCM Publisher 1 source file to be analyzed
[INFO] SCM Publisher 1/1 source file have been analyzed (done) | time=116ms
[INFO] CPD Executor 4 files had no CPD blocks
[INFO] CPD Executor Calculating CPD for 14 files
[INFO] CPD Executor CPD calculation finished (done) | time=18ms
[INFO] SCM writing changed lines
[INFO] Merge base sha1: 1072e799562a42b975b41bc394b7c5cbec18b4c0
[INFO] SCM writing changed lines (done) | time=50ms
[INFO] Analysis report generated in 181ms, dir size=190.3 kB
[INFO] Analysis report compressed in 35ms, zip size=35.2 kB
[INFO] Analysis report uploaded in 36ms
[INFO] ------------- Check Quality Gate status
[INFO] Waiting for the analysis report to be processed (max 300s)
[INFO] QUALITY GATE STATUS: PASSED - View details on https://sonarqube.poppulo-tooling.com/dashboard?id=sre_research_channels-groups-sonar-test_AYi7dGQm__4FIJKXUhIO&pullRequest=24
[INFO] Analysis total time: 22.612 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  31.414 s
[INFO] Finished at: 2023-06-14T20:06:46Z
[INFO] ------------------------------------------------------------------------

Hi,

Welcome to the community!

Is there more to that log from GitLab? Because the snippet you’ve posted doesn’t include the processing of the coverage report.

 
Ann

Hello Ann, thanks for the response. Please find attached a more detailed snippet of the gitlab logs.

[INFO] User cache: [____]
[INFO] SonarQube version: 10.0.0.68432
[INFO] Default locale: "en_US", source code encoding: "US-ASCII" (analysis is platform dependent)
[INFO] Load global settings
[INFO] Load global settings (done) | time=209ms
[INFO] Server id: [____]
[INFO] User cache: [____]
[INFO] Load/download plugins
[INFO] Load plugins index
[INFO] Load plugins index (done) | time=79ms
[INFO] Load/download plugins (done) | time=11617ms
[INFO] Loaded core extensions: developer-scanner
[INFO] Process project properties
[INFO] Process project properties (done) | time=15ms
[INFO] Execute project builders
[INFO] Execute project builders (done) | time=2ms
[INFO] Project key: [____]
[INFO] Base dir: [____]
[INFO] Working dir: [____]
[INFO] Load project settings for component key: [____]
[INFO] Load project settings for component key: [____] (done) | time=19ms
[INFO] Load project branches
[INFO] Load project branches (done) | time=18ms
[INFO] Load branch configuration
[INFO] Detected branch/PR in 'GitLab'
[INFO] Auto-configuring branch 'master'
[INFO] Load branch configuration (done) | time=7ms
[INFO] Auto-configuring with CI 'Gitlab CI'
[INFO] Load quality profiles
[INFO] Load quality profiles (done) | time=65ms
[INFO] Load active rules
[INFO] Load active rules (done) | time=4233ms
[INFO] Load analysis cache
[INFO] Load analysis cache (404) | time=10ms
[INFO] Branch name: master
[INFO] Load project repositories
[INFO] Load project repositories (done) | time=30ms
[INFO] Indexing files...
[INFO] Project configuration:
[INFO]   Excluded sources: [____]
[INFO] 26 files indexed
[INFO] 0 files ignored because of inclusion/exclusion patterns
[INFO] 0 files ignored because of scm ignore settings
[INFO] Quality profile for java: Sonar way (outdated copy)
[INFO] Quality profile for xml: Sonar way
[INFO] ------------- Run sensors on module [____]
[INFO] Load metrics repository
[INFO] Load metrics repository (done) | time=36ms
[INFO] Sensor JavaSensor [java]
[INFO] Configured Java source version (sonar.java.source): 8
[INFO] JavaClasspath initialization
[INFO] JavaClasspath initialization (done) | time=9ms
[INFO] JavaTestClasspath initialization
[INFO] JavaTestClasspath initialization (done) | time=4ms
[INFO] Server-side caching is enabled. The Java analyzer will not try to leverage data from a previous analysis.
[INFO] Using ECJ batch to parse 18 Main java source files with batch size 102 KB.
[INFO] Starting batch processing.
[INFO] The Java analyzer cannot skip unchanged files in this context. A full analysis is performed for all files.
[INFO] 100% analyzed
[INFO] Batch processing: Done.
[INFO] Did not optimize analysis for any files, performed a full analysis for all 18 files.
[INFO] Using ECJ batch to parse 7 Test java source files with batch size 102 KB.
[INFO] Starting batch processing.
[INFO] 100% analyzed
[INFO] Batch processing: Done.
[INFO] Did not optimize analysis for any files, performed a full analysis for all 7 files.
[INFO] No "Generated" source files to scan.
[INFO] Sensor JavaSensor [java] (done) | time=3117ms
[INFO] Sensor C# Project Type Information [csharp]
[INFO] Sensor C# Project Type Information [csharp] (done) | time=1ms
[INFO] Sensor C# Analysis Log [csharp]
[INFO] Sensor C# Analysis Log [csharp] (done) | time=35ms
[INFO] Sensor C# Properties [csharp]
[INFO] Sensor C# Properties [csharp] (done) | time=1ms
[INFO] Sensor SurefireSensor [java]
[INFO] parsing [[____]/target/surefire-reports]
[INFO] Sensor SurefireSensor [java] (done) | time=153ms
[INFO] Sensor HTML [web]
[INFO] Sensor HTML [web] (done) | time=5ms
[INFO] Sensor XML Sensor [xml]
[INFO] 1 source file to be analyzed
[INFO] 1/1 source file has been analyzed
[INFO] Sensor XML Sensor [xml] (done) | time=545ms
[INFO] Sensor TextAndSecretsSensor [text]
[INFO] 26 source files to be analyzed
[INFO] 26/26 source files have been analyzed
[INFO] Sensor TextAndSecretsSensor [text] (done) | time=147ms
[INFO] Sensor VB.NET Project Type Information [vbnet]
[INFO] Sensor VB.NET Project Type Information [vbnet] (done) | time=1ms
[INFO] Sensor VB.NET Analysis Log [vbnet]
[INFO] Sensor VB.NET Analysis Log [vbnet] (done) | time=20ms
[INFO] Sensor VB.NET Properties [vbnet]
[INFO] Sensor VB.NET Properties [vbnet] (done) | time=0ms
[INFO] Sensor JaCoCo XML Report Importer [jacoco]
[INFO] Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
[INFO] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=80ms
[INFO] Sensor CSS Rules [javascript]
[INFO] No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
[INFO] Sensor CSS Rules [javascript] (done) | time=1ms
[INFO] Sensor ThymeLeaf template sensor [securityjavafrontend]
[INFO] javasecurity:S5131 is not activated in quality profile: skipping execution of thymeleaf sensor.
[INFO] Sensor ThymeLeaf template sensor [securityjavafrontend] (done) | time=1ms
[INFO] Sensor IaC Docker Sensor [iac]
[INFO] 0 source files to be analyzed
[INFO] 0/0 source files have been analyzed
[INFO] Sensor IaC Docker Sensor [iac] (done) | time=161ms
[INFO] Sensor Serverless configuration file sensor [security]
[INFO] 0 Serverless function entries were found in the project
[INFO] 0 Serverless function handlers were kept as entrypoints
[INFO] Sensor Serverless configuration file sensor [security] (done) | time=6ms
[INFO] Sensor AWS SAM template file sensor [security]
[INFO] Sensor AWS SAM template file sensor [security] (done) | time=2ms
[INFO] Sensor AWS SAM Inline template file sensor [security]
[INFO] Sensor AWS SAM Inline template file sensor [security] (done) | time=1ms
[INFO] Sensor javabugs [dbd]
[INFO] Reading IR files from: [____]/sonar/ir/java
[INFO] No IR files have been included for analysis.
[INFO] Sensor javabugs [dbd] (done) | time=4ms
[INFO] Sensor pythonbugs [dbd]
[INFO] Reading IR files from:[____]/sonar/ir/python
[INFO] No IR files have been included for analysis.
[INFO] Sensor pythonbugs [dbd] (done) | time=0ms
[INFO] Sensor JavaSecuritySensor [security]
[INFO] No taint analysis rules have been enabled, will not execute taint analysis.
[INFO] Sensor JavaSecuritySensor [security] (done) | time=1ms
[INFO] Sensor CSharpSecuritySensor [security]
[INFO] Reading type hierarchy from: [____]/ucfg_cs2
[INFO] Read 0 type definitions
[INFO] No UCFGs have been included for analysis.
[INFO] Sensor CSharpSecuritySensor [security] (done) | time=3ms
[INFO] Sensor PhpSecuritySensor [security]
[INFO] Reading type hierarchy from: [____]/sonar/ucfg2/php
[INFO] Read 0 type definitions
[INFO] No UCFGs have been included for analysis.
[INFO] Sensor PhpSecuritySensor [security] (done) | time=4ms
[INFO] Sensor PythonSecuritySensor [security]
[INFO] Reading type hierarchy from: [____]/ucfg2/python
[INFO] Read 0 type definitions
[INFO] No UCFGs have been included for analysis.
[INFO] Sensor PythonSecuritySensor [security] (done) | time=0ms
[INFO] Sensor JsSecuritySensor [security]
[INFO] Reading type hierarchy from: [____]/ucfg2/js
[INFO] Read 0 type definitions
[INFO] No UCFGs have been included for analysis.
[INFO] Sensor JsSecuritySensor [security] (done) | time=1ms
[INFO] ------------- Run sensors on project
[INFO] Sensor Analysis Warnings import [csharp]
[INFO] Sensor Analysis Warnings import [csharp] (done) | time=1ms
[INFO] Sensor Zero Coverage Sensor
[INFO] Sensor Zero Coverage Sensor (done) | time=16ms
[INFO] Sensor Java CPD Block Indexer
[INFO] Sensor Java CPD Block Indexer (done) | time=85ms
[INFO] CPD Executor 4 files had no CPD blocks
[INFO] CPD Executor Calculating CPD for 14 files
[INFO] CPD Executor CPD calculation finished (done) | time=16ms
[INFO] Load New Code definition
[INFO] Load New Code definition (done) | time=17ms
[INFO] Analysis report generated in 139ms, dir size=332.7 kB
[INFO] Analysis report compressed in 80ms, zip size=103.1 kB
[INFO] Analysis report uploaded in 54ms
[INFO] ------------- Check Quality Gate status
[INFO] Waiting for the analysis report to be processed (max 300s)
[INFO] QUALITY GATE STATUS: PASSED - View details on [____]
[INFO] Analysis total time: 25.284 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  48.707 s
[INFO] Finished at: 2023-07-27T13:30:02Z
[INFO] ------------------------------------------------------------------------
[WARNING] 
[WARNING] Plugin validation issues were detected in 1 plugin(s)
[WARNING] 
[WARNING]  * org.sonarsource.scanner.maven:sonar-maven-plugin:3.5.0.1254
[WARNING] 
[WARNING] For more or less details, use 'maven.plugin.validation' property with one of the values (case insensitive): [BRIEF, DEFAULT, VERBOSE]

Hi @angom,

Welcome to the community!

I guess you work with the OP?

Here’s what we’ve got in the logs:

[INFO] Sensor JaCoCo XML Report Importer [jacoco]
[INFO] Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
[INFO] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=80ms

So, as y’all said initially, the report is being imported, and without errors. If you want more detail, you should enable debug logging (-X for Maven, I believe).

BTW, test count is entirely unrelated. It comes from a different report.

 
Ann

Hello Ann thanks for the help on this. Yes I work with with the OP.
I have enabled debug logging and this is what we got in the “Log-with-maven-debug.txt”. We also had a look at the jacoco report and it looks good. I have also attached a snippet of the html report.
image
Log-with-maven-debug.txt (69.8 KB)

Hi,

Here’s what we’ve got in the log:

[INFO] 15:37:22.634 Sensor JaCoCo XML Report Importer [jacoco]
[INFO] 15:37:22.641 Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
[DEBUG] 15:37:22.641 Reading report [___]jacoco.xml'
[INFO] 15:37:22.693 Sensor JaCoCo XML Report Importer [jacoco] (done) | time=59ms

So it looks like the report is imported without error. I guess it’s time for you to read that report and see how it correlates with your project. Because I find this suspicious:
Selection_1279

I read that as 0% coverage, altho of what, I don’t know.

 
Ann

Hi Ann. I work with @ angom and @ pvasilev94 . I’ll be looking into this issue now.

The 0% coverage you outlined is for one of the java packages analysed by sonarqube in our project. You can see the overall coverage is 68%

The XML report also shows matching overall coverage (<counter type="INSTRUCTION" missed="564" covered="1200" />), yet Sonarqube reports 0%.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.1//EN" "report.dtd">
<report name="Channels Groups Service">
	<sessioninfo id="runner-k56ddd4--project-1119-concurrent-0-35ba2f93" start="1691076927898" dump="1691076935187" />
	<package name="com/foo/channels/groups/config">
	</package>
	<package name="com/foo/channels/groups/model">
	</package>
	<package name="com/foo/channels/groups/auth">
	</package>
	<package name="com/foo/channels/groups/validation">
	</package>
	<package name="com/foo/channels/groups/services">
	</package>
	<package name="com/foo/channels/groups/handlers">
	</package>
	<package name="com/foo/channels/groups/consumers">
	</package>
	<package name="com/foo/channels/groups/utilities">
	</package>
	<counter type="INSTRUCTION" missed="564" covered="1200" />
	<counter type="BRANCH" missed="9" covered="65" />
	<counter type="LINE" missed="150" covered="321" />
	<counter type="COMPLEXITY" missed="67" covered="106" />
	<counter type="METHOD" missed="59" covered="77" />
	<counter type="CLASS" missed="11" covered="15" />

In summary:

  • sonarqube is reading the correct xml file at target/site/jacoco/jacoco.xml.
  • The file at target/site/jacoco/jacoco.xml contains the above report metrics
  • Sonar UI reports 0% coverage

Hi,

Okay, I’ve flagged this for the language experts.

 
Ann

1 Like

Hi @Matt_Byrne,

Did you have a chance to take a look at Java test coverage?
What is the command you run analysis with?

All the best,

Irina

Hi Irina

To run coverage we run

    - mvn clean install
    - mvn package

And for analysis we run mvn sonar:sonar

Our pom.xml contains the following:

        <profile>
            <id>sonar</id>
            <activation><activeByDefault>true</activeByDefault></activation>
            <properties>
                <sonar.host.url>
                    --redacted--
                </sonar.host.url>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.8.10</version>
                        <executions>
                            <execution>
                                <id>prepare-agent</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>report</id>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.sonarsource.scanner.maven</groupId>
                        <artifactId>sonar-maven-plugin</artifactId>
                        <version>3.5.0.1254</version>
                    </plugin>
                </plugins>
            </build>
        </profile>

As you can see from the thread above, it looks like our sonar analysis is reading the correct report, and is reporting on lines of code, coverage etc. Does sonarqube use <counter type="INSTRUCTION" missed="564" covered="1200" /> to report on the code coverage?

Hi @Matt_Byrne ,

As noted in the documentation, can you please try to run an analysis with mvn sonar:sonar -Pcoverage

All the best,

Irina

Thanks Irina.

I made that change too, but logging warned that the “coverage” profile didn’t exist:

[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "coverage" could not be activated because it does not exist.
[WARNING] 
[WARNING] Plugin validation issues were detected in 2 plugin(s)
[WARNING] 
[WARNING]  * org.apache.maven.plugins:maven-site-plugin:3.12.1
[WARNING]  * org.codehaus.mojo:sonar-maven-plugin:3.9.1.2184
[WARNING] 
[WARNING] For more or less details, use 'maven.plugin.validation' property with one of the values (case insensitive): [BRIEF, DEFAULT, VERBOSE]
[WARNING] 

Yes, looking at your pom.xml, it makes sense it doesn’t work.
Can you please take a closer look at the documentation? It is explained there.

You need to add a profile with the id coverage. Something like the following:

<profile>
  <id>coverage</id>
  <build>
   <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
     <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.7</version>
      <executions>
        <execution>
          <id>prepare-agent</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <execution>
          <id>report</id>
          <goals>
            <goal>report</goal>
          </goals>
          <configuration>
            <formats>
              <format>XML</format>
            </formats>
          </configuration>
        </execution>
      </executions>
    </plugin>
    ...
   </plugins>
  </build>
</profile>

Hi Irina.

I’ve added the coverage profile ID, but still Sonar reports zero coverage

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <profiles>
        <profile>
            <id>coverage</id>
            <activation><activeByDefault>true</activeByDefault></activation>
            <properties>
                <sonar.host.url>
                    https://sonarqube.poppulo-tooling.com/
                </sonar.host.url>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.8.10</version>
                        <executions>
                            <execution>
                                <id>prepare-agent</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>report</id>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.sonarsource.scanner.maven</groupId>
                        <artifactId>sonar-maven-plugin</artifactId>
                        <version>3.5.0.1254</version>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
.
.
.
</project>

Does sonarqube use <counter type="INSTRUCTION" missed="564" covered="1200" /> from the XML to report on the code coverage?

So this turned out to be a java package was seperated in the codebase with dots (e.g. com.company.my.package), which lead to SonarQube not being able to find the code. The resolution was to rename the java package to be seperated by slashes (i.e. com/company/my/package) in the codebase. The package name is still seperated by dots, but now SonarQube correctly finds the files within the codebase and shows the code coverage

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.