21

I want to use code coverage tools(lcov) in my cmake project. I read the example here https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake The tests are added in my project using the 'add_test()' cmake function.

I want to create a custom target, something called 'test_coverage', that when invoked for execution should run all the tests, collect their coverage data and generate the html(using genhtml) in a directory 'code_coverage'.

Is there a way I could get the list of all the tests in my project and their directory paths, so that in the custom target 'test_coverage' I could execute each test individually and collect its coverage data ?

David Doria
  • 9,383
  • 15
  • 81
  • 143
Monku
  • 2,168
  • 2
  • 25
  • 51
  • 1
    From your decription I assume you have already read [Detailed guide on using gcov with CMake/CDash?](http://stackoverflow.com/questions/13116488/detailed-guide-on-using-gcov-with-cmake-cdash). But did you see e.g. https://github.com/stepcode/stepcode/blob/master/lcov.cmake ? `CTest` knows/can extract the list of test in your CMake's binary output directory and with its script mode `ctest -S ...` you can automate the whole build, test and collect coverage data process. – Florian Jun 10 '15 at 19:00
  • 1
    Also useful could be [How to run ctest after building my project with cmake](http://stackoverflow.com/questions/15115075/how-to-run-ctest-after-building-my-project-with-cmake) – Florian Jun 10 '15 at 19:01
  • @Florian I don't see a ctest_coverage call in the lcov.cmake example? Also, are you saying you can perform the coverage analysis without a script (-S) at all? Can you point to an example of how to do that? I'm interested in coverage analysis of all of the test targets in aggregate. – David Doria Feb 02 '16 at 02:09
  • Maybe this module which we are using is useful for you: https://code.cor-lab.de/projects/rsc/repository/entry/cmake/Modules/EnableCoverageReport.cmake. It uses lcov and gcovr to generate the reports. It still doesn't help to automatically collect the test targets, but maybe it saves some time to write your coverage logic. – languitar Jun 14 '16 at 12:04
  • This is now answered here: https://stackoverflow.com/questions/13116488/detailed-guide-on-using-gcov-with-cmake-cdash/16536401#16536401 – Philipp Ludwig Feb 28 '18 at 09:12

1 Answers1

2

You can either execute 'ctest -VV' from the command line, and if all tests were created using add_test, all will execute.

If you want a custom build target to do the same, you can use this code:

add_custom_target(run_tests
   COMMAND "ctest -VV" )

I have a LOT of cmake code for code coverage and unit testing to show, but it doesn't make sense to copy/paste here yet since it sounds like you're just getting started.