Generating coverage reports using JUCE Unit test framework

Hello everyone,

I’m using Projucer to create a custom project which I run in xcode. For unit testing my code I turned to these classes JUCE: UnitTest Class Reference and they are doing the job for now.

I’d like to have a coverage report (to more easily spot which lines/cases of a given class aren’t tested) but i can’t seem to find any way to export a proper XML from these tests, is there a way to generate a coverage report?

Is the answer to switch to some other UT solution for obtaining any kind of a report?

Hello lazzo!

I think that the usual way of generating these coverage reports is to compile the unit-test binary with some instrumentation flags. This will put special instructions into the binary, which will emit the coverage information at runtime. Here’s how we do it:

  • Compile the unit-test target with the following flags: -fprofile-instr-generate -fcoverage-mapping
  • Run the unit test
  • Locate the generated default.profraw file in the working directory
    This is a intermediate coverage format that will need to be converted in order to be human-readable:
  • xcrun llvm-profdata merge -sparse default.profraw -o default.profdata
  • xcrun llvm-cov report <test-binary> -instr-profile=default.profdata

If you’re using CLion as IDE, it can visually mark the covered areas in the code if you’re unit-test was compiled with the appropriate compiler flags.

You can find more information here:

https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
https://llvm.org/docs/CommandGuide/llvm-cov.html

1 Like

I do it slightly differently with the --coverage flag: tracktion_engine/tests/coverage at develop · Tracktion/tracktion_engine · GitHub

Then use lcov and genhtml to convert to HTML:

lcov --capture --directory "${CMAKE_BUILD_DIR}/" -exclude "*juce_*" -exclude "*3rd_Party*" -exclude "*3rd_party*" -exclude "/Applications*" -exclude "/usr*" -exclude "*examples*" --output-file "$COVERAGE_FILE"
genhtml "$COVERAGE_FILE" --output-directory ${HTML_DIR}

You can also use codecoc for free if it’s an open source project.

1 Like

Interesting, i didn’t realize that clang supported the gcov format too, thats neat!