Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see code coverage in Clion

I need to be able to check how well my C++ code covered by unit test in critical places. I'm using Clion as IDE based on Cmake project structure (not sure if something else supported). Is there any ways to get code coverage info with Clion?

like image 450
silent_coder Avatar asked Dec 16 '16 12:12

silent_coder


People also ask

How do I display code coverage?

Code coverage option is available under the Test menu when you run test methods using Test Explorer. The results table shows the percentage of the code executed in each assembly, class, and procedure. The source editor highlights the tested code.

How do I show code coverage in IntelliJ?

Coverage in the Coverage tool window If you want to reopen the Coverage tool window, select Run | Show Code Coverage Data from the main menu, or press Ctrl+Alt+F6 . The report shows the percentage of the code that has been executed or covered by tests. You can see the coverage result for classes, methods, and lines.

Why code coverage is not showing in IntelliJ?

From the main menu, select Run | Show Coverage Data ( Ctrl+Alt+F6 ). In the Choose Coverage Suite to Display dialog, select the checkboxes next to the necessary suites, and click Show selected. IntelliJ IDEA opens the coverage results for the selected test suites.


2 Answers

There is no such feature in CLion for now. The feature request exists. Also we are unaware of any existing plugin for code coverage in CLion.

like image 155
nastasiak2512 Avatar answered Nov 02 '22 15:11

nastasiak2512


It is possible with the latest vanilla versions of CLion (e.g. 2020.1.1) - no plugins required.


DETAILS

See official doc.

For example, on Linux (Fedora 31):

  • Depending on compiler, ensure CLion picks the right toolchain:

    Even though CMakeLists.txt sets compiler for the build, it may disagree with the one chosen by IDE to show the coverage (something to be improved).

  • Obviously, install necessary tools outside of IDE and ensure their versions match:

    sudo dnf install clang llvm # ...
    sudo dnf update
    

gcc

# CMakeLists.txt
set(CMAKE_C_COMPILER cc)
set(CMAKE_CXX_COMPILER c++)
set(COMPILE_FLAGS "--coverage")
set(CMAKE_EXE_LINKER_FLAGS "--coverage")

File / Settings... / Build, Execution, Deployment / Toolchain

gcc


clang

# CMakeLists.txt
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(COMPILE_FLAGS "-fprofile-instr-generate -fcoverage-mapping")
set(CMAKE_EXE_LINKER_FLAGS "-fprofile-instr-generate")

File / Settings... / Build, Execution, Deployment / Toolchain

clang

like image 32
uvsmtid Avatar answered Nov 02 '22 15:11

uvsmtid