Introduction
In the blog post, I would like to share how I include test coverage report in all my cpp projects. At the end of this post, your cpp project should be able to generate the following html page automatically after running the test cases.
There are many resource talking about how to read the result generated by the lcov [1], I am not going to repeat in this blog since the purpose of this blog is focusing on the tool setup.
Example
Here I have prepared an example to show how to get a test coverage report for a cpp project. All the prerequisite packages and g++ compiler flags are included in this project. You can find them from the docker file and the cmake files.
# download the example cpp project as a demo $ git clone https://gitlab.com/SulfredLee/cpp_showcases.git # prepare dev docker image $ cd cpp_showcases/dockerEnv $ ./BuildImageDev.sh # run the dev image $ cd dev $ ./start_dev_container.sh # prepare build script in the container $ cd ./scripts $ ./CCMake_Debug.sh # a debug mode build script is generated $ cd ../debug $ ninja # once the build is done, you can get the test coverage report $ cd ../scripts $ ./View_Test_Coverage.sh # view the html $ pwd /cpp/project/test_coverage $ ls -hal ... index.html ... # DONE, you can view the index.html with your favorite browser
Highlights
In this section, I will highlight some preparation steps that you might need to be aware of.
In the CMakeLists.txt, you can find the additional flags for the test coverage enable [2] .
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3 -fprofile-arcs -ftest-coverage")
To be able to generate the html coverage report successfully, you need to install the lcov first [4].
$ sudo apt-get install lcov
Reference
[1] A. R. (2022, February 17). Testing code coverage in C using GCOV. YouTube. https://www.youtube.com/watch?v=UOGMNRcV9-4
[2] Oracle Linux 6: Porting Guide. (2021, March 31). https://docs.oracle.com/en/operating-systems/oracle-linux/6/porting/ch02s05s01.html
[3] google-test: code coverage. (n.d.). Stack Overflow. https://stackoverflow.com/questions/2359344/google-test-code-coverage
[4] google-test: code coverage. (n.d.). Stack Overflow. https://stackoverflow.com/a/36980590/2358836



Comments
Post a Comment