Skip to content

An example C++ repository built with CMake on Linux using GitHub Actions and analyzed on SonarCloud (test coverage by gcov)

Notifications You must be signed in to change notification settings

sonarsource-cfamily-examples/linux-autotools-gcov-travis-sc

Repository files navigation

C++ example project for generating test coverage with gcov

Build Status Quality Gate Status

The test coverage shown here works on both SonarQube and SonarCloud. This project is analysed on SonarCloud!

ℹ️
This simple example aims at demonstrating code coverage in action. It explains how to add coverage on a project for which the analysis is already configured. Analysis configuration examples are available here.
Adding code coverage is done the same way regardless of whether you use the Build Wrapper or a Compilation Database to configure the analysis.

To get coverage and feed it to the sonar-scanner, use the following procedure.

  1. Add the --coverage and -fprofile-abs-path compilation flags to instrument the binaries

  2. Run the tests to generate the test coverage information

  3. Run gcov --preserve-paths from a dedicated directory for each object file

  4. Point the sonar.cfamily.gcov.reportsPath property at that dedicated directory with coverage reports

  5. Run sonar-scanner as the final step

You can take a look at the sonar-project.properties and .travis.yml to see it in practice.

This project uses gcov to collect test coverage

For SonarCloud to pick-up the coverage information produced from compiling with the --coverage flag and running the tests, you need to run gcov for each object file and point sonar.cfamily.gcov.reportsPath property to the folder containing the produced .gcov files.

⚠️
Beware that if you are running multiple executables and want to collect coverage from all of them, you have to be careful of keeping one .gcda file per source file and avoid overwriting .gcov files (especially if you are building out-of-source, for example, with CMake). See also -fprofile-abs-path gcc option and --preserve-paths gcov option.

Documentation

Code Description

An example of a flawed C++ code. The code repository can be analyzed automatically, but it can also be compiled with different build systems using different CI pipelines on Linux, macOS, and Windows.

The code repository is forked into other repositories in this collection to add a specific build system, platform, and CI. The downstream repositories are analyzed either with SonarQube or SonarCloud.

You can find examples for:

Using the following build systems:

Running on the following CI services:

Configured for analysis on:

You can find also a few examples demonstrating:

See examples-structure.adoc for a description of the structure of this GitHub organization and the relations between its different repositories.