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, and it computes the union of the unit and integration test results.

  1. Unit tests
    It tests the classes or methods directly, generally by using specific test framework (Junit, testNG, etc ...)
    The "container framework" used behind the scene (Spring/ JBoss/ EJB or whatever) SHOULD NOT be used in the Unit test scope.
    External calls to test servers or third party components/anything MUST NOT be done here. These type of tests should be executable by anybody locally in an IDE.
    Due to that, it's sometimes required to make use of mock frameworks to replace the code that can't be triggered because it does require unreachable components
    The mocking SHOULD NEVER consume the developer's bandwidth, that means mocking the code should be straight-forward.
    Don't waste your time into a complicated implementation, for that there is a second test phase called "integration tests"

    Also DO NOT add "isTest" flag in your code to replace the mock, this is a common test anti pattern that lead the useless tests.

  2. Integration tests
    It tests the code within the "container framework" and can make use of 3rd party components, like databases.
    The 3rd party components could be emulated, or even better, could be physically mounted locally, with the help of docker for instance.
    The starting of the those 3rd party components can be initiated/shut-downed by the build itself in a "Pre-integration/Post-integration" phase.

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

Code Block
languagexml
<plugin>
	<groupId>org.sonarsource.scanner.maven</groupId>
	<artifactId>sonar-maven-plugin</artifactId>
  	<version>x.x</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

Code Block
languagexml
<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/properties that must be included in the pom.xml to execute the tests and record the coverage

    Properties

    Code Block
    languagexml
    <properties>
    ...
                    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
                    <sonar.surefire.reportsPath>${project.build.directory}/surefire-reports</sonar.surefire.reportsPath>
                    <sonar.jacoco.reportPath>${project.build.directory}/coverage-reports/jacoco.exec</sonar.jacoco.reportPath>
                    <sonar.jacoco.itReportPath>${project.build.directory}/coverage-reports/jacoco-it.exec</sonar.jacoco.itReportPath>
                    <sonar.jacoco.reportMissing.force.zero>true</sonar.jacoco.reportMissing.force.zero>
                    <sonar.projectVersion>${project.version}</sonar.projectVersion>
                    <!-- Enable language to disable other language analysis -->
                    <sonar.language>java</sonar.language>
                    <sonar.exclusions>src/main/resources/**</sonar.exclusions>
    ...
    </properties>

    Surefire (Unit tests)

    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)

    Code Block
    languagexml
    <plugin>
    	<groupId>org.apache.maven.plugins</groupId>
    	<artifactId>maven-surefire-plugin</artifactId>
    	<version>X.XX.X</version>
    	<configuration>
    		<forkCount>1</forkCount>
    		<reuseForks>true</reuseForks>
    		<useSystemClassLoader>false</useSystemClassLoader>
    	</configuration>
    </plugin>

    Failsafe (Integration tests)

    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)

    Code Block
    languagexml
    <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>


    Maven properties

  • Javascript

  • Python

    Section TBD