Search code examples
c++unit-testingcontinuous-integrationcode-coverage

how can i get c++ code overage from google test suite in terminal?


I have started using the Google Test unit testing tools which I am building into a CI pipeline. Is there a code coverage tool that runs in the shell and would allow me to set thresholds and add as a job into the pipeline?

For reference I come from a NodeJS background and use a pipeline as follows:

  1. linter (eslint)
  2. unit tests (jasmine)
  3. code coverage (istanbul coverage && istanbul check-coverage)

The bit i'm struggling with is the third step. In NodeJS I can set the acceptable thresholds and the job fails if these are not met.

I was hoping to replicate this for my C++ code. Is this even possible?


Solution

  • Code coverage is not linked to the test framework you use.

    With C++ on Linux, you have to compile your software with special flags to enable the code coverage, e.g. with g++ you have to set the argument --coverage (and disabling all optimisations is also recommended).

    When you then run the test programs, you will get a lot of files with the coverage data in them. These can then be collected and evaluated by e.g. lcov.
    lcov can create HTML pages with the result, but is also prints the totals of the coverage analysis to stdout. So, you would have to build a script that runs lcov, filters the output and reports error or failure depending on the percentage measured.

    Btw, you can set limits for lcov to define when the coverage is sufficient or not, but this is only used for the background color in the HTML output.

    On each of these topics you'll find multiple entries here at Stackoverflow, how these tasks can be accomplished.