Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

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
    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

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>
      <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
  • No labels