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?
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.
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.
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.
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.
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
# 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
# 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With