Is it possible to get code coverage done by tests using google test framework?
3 Answers
Yes, I've successfully used both free (gcov) and commercial (CTC++) tools. No special steps are needed, just follow the documentation.
More details can be found in this blog http://googletesting.blogspot.dk/2014/07/measuring-coverage-at-google.html

- 302
- 1
- 8

- 8,358
- 3
- 33
- 38
-
Don't forget that Lcov is a nice GUI for Gcov - http://ltp.sourceforge.net/coverage/lcov.php and take a look also at http://ggcov.sourceforge.net/index.html – Mawg says reinstate Monica Sep 09 '15 at 08:35
-
29The link does not explain how to integrate gcov with gtest. – Arun Jun 21 '17 at 22:32
-
I do not see the implementation. – user2148571 Dec 21 '17 at 20:20
Yes, You can club your Gtest Based application with support of Gcov/lcov. refer the documentation of lcov http://ltp.sourceforge.net/coverage/lcov.php
there is one linux test project utility available which does your job very easy and is very self-interpretative.
lcov
- a graphical GCOV front-end
Download from Ubuntu repo:
$ sudo apt-get install lcov
Use following commands in your build directory
$ lcov --directory ./build/ --capture --output-file ./code_coverage.info -rc lcov_branch_coverage=1
Run the Application
Generate HTML Report
$ genhtml code_coverage.info --branch-coverage --output-directory ./code_coverage_report/
This will look something like - http://ltp.sourceforge.net/coverage/lcov/output/index.html

- 310
- 2
- 5
-
As an addition to your answer: Using CMake, I had to add the following lines to CMakeLists.txt to get the right compiler and linker flags when building the tests: SET(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage") SET(GCC_COVERAGE_LINK_FLAGS "-lgcov") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}") SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}") (Credits: https://stackoverflow.com/a/11797272) Then, the tests had to be run once before executing lcov to get the correct results. – DocRobson Jan 05 '23 at 15:28
(Only works on Windows)
It seems this question is still active, so here's an additional option. I've just tested Google Tests on our code coverage solution (yes, I'm the author), which works on Visual Studio. The project can be found at: https://github.com/atlaste/CPPCoverage
Instructions: right-click project, run coverage. :-)
If you prefer an XML output (for CI), you can call the coverage executable manually.

- 30,418
- 3
- 57
- 87
-
2@kevr How exactly? Let me spell it out: "Is it possible to get code coverage done by tests using google test framework?" -- yes you can, if you're using this tool, and if you are happy with the [VS/Windows] constraints of the tool. I'd say it's spot on. – atlaste Apr 03 '20 at 09:49
-
It's off topic. He was asking generically, not about Visual Studio on Windows. An answer to the generic question should be one with an answer that can apply generically. – kevr Apr 09 '20 at 23:03
-
8@kevr If that's your definition, then my answer here is just as much off-topic as all the other answers here. I mean come on, one of them actually has a "download from ubuntu repo" in the answer - how is that any more generic or on-topic than my answer? Fact is that my answer provides exactly what the OP is asking with just one installation and two clicks. – atlaste Apr 10 '20 at 09:07