Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Sonar requires the coverage files to be provided during the project scanning. Those data files are generally recorded by plugins, servers, interpreters, ... when executing the tests and made accessible to the sonar scanner.

This depends of the language and packager (like maven/gradle) used in the project to execute the tests and build the artifacts.

Sonar recognizes 2 types of testing that can be executed in a build for the coverage report.

  1. Unit tests
    This tests the classes or methods directly generally by using specific test framework (Junit, testNG, etc ...)
    External calls to test servers for third party components or anything MUST NOT be done here. These type of tests should be executable on a
    This can require the usage of mock frameworks because databases or third party systems MUST NOT be
  2. Integration tests

Here are some tips to setup that coverage per packager and per language

For Maven

When using MAVEN, at least one pom.xml must be defined, the project contains normally a central pom.xml that is used to build the entire project (it can contain multiple maven sub-modules)

In this pom.xml, the sonar plugin must be defined

        <plugin>
           <groupId>org.sonarsource.scanner.maven</groupId>
           <artifactId>sonar-maven-plugin</artifactId>
           <version>3.2</version>
</plugin>

There is also a need to setup some properties for SONAR in order to indicate how the scans should be done, this include the coverage data files that must be used

<properties>

....

    <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
    <!-- Default Sonar configuration -->
    <sonar.jacoco.reportPath>target/code-coverage/jacoco-ut.exec</sonar.jacoco.reportPath>
    <sonar.jacoco.itReportPath>target/code-coverage/jacoco-it.exec</sonar.jacoco.itReportPath>

....

</properties>

Note that if you import the org.onap.oparent.oparent those settings are already included, so there is no need to add them unless you want to override those settings

  • Java

    For java there are different plugins that must be included in the pom.xml,

    Surefire

    This one is used to execute the unit tests, various config are available depending of the project need (https://maven.apache.org/surefire/maven-surefire-plugin/index.html)

    <plugin> 
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>X.XX.X</version>

...

        </plugin>
  • Java
      <configuration>
       <forkCount>1</forkCount>
       <reuseForks>true</reuseForks>
       <useSystemClassLoader>false</useSystemClassLoader>
      </configuration>
    </plugin>

    Failsafe

    This one is used to execute the integration tests, various config are available depending of the project need (https://maven.apache.org/surefire/maven-failsafe-plugin/index.html)

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>X.XX.X</version>
        <executions>
          <execution>
             <id>integration-tests</id>
             <goals>
               <goal>integration-test</goal>
               <goal>verify</goal>
             </goals>
             <configuration>
               <additionalClasspathElements>
                  <additionalClasspathElement>${project.build.directory}/classes</additionalClasspathElement>
               </additionalClasspathElements>
               <includes>
                 <include>**/*ItCase.java</include>
               </includes>
               <forkCount>1</forkCount>
               <reuseForks>false</reuseForks>
               <useSystemClassLoader>false</useSystemClassLoader>
             </configuration>
          </execution>
        </executions>
    </plugin>
  • Javascript

  • Python

    Section TBD