How to see code coverage in Clion
Asked Answered
P

2

9

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?

Pungent answered 16/12, 2016 at 12:53 Comment(2)
Search for it in the CLion issue tracker. If the request doesn't exist yet, add one. Or create a plugin for it?Cerenkov
Maybe gcov will work. There is an article by Google on how they used it to get code coverage with gtest. testing.googleblog.com/2014/07/… Basically you run gcov and it will tell you which lines of code executed how many times. So, you can run a test and check its coverage or run all the tests and find code that is not checked.Hollands
B
7

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.

Botticelli answered 17/12, 2016 at 20:32 Comment(0)
D
2

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

Direction answered 1/5, 2020 at 4:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.