Skip to content

Commit a435441

Browse files
committedDec 3, 2014
Add support for generating coverage statistics for tests
Build option ENABLE_COVERAGE must be set. There's also a secondary option GENERATE_COVERAGE_DOCS which requires lcov and will generate html docs of unit test coverage. To generate coverage, first enable the cmake options, then build QGIS. From the build directory, run "make qgis_coverage". You can also add command line arguments to "make qgis_coverage" which are forwarded on to ctest. This allows for skipping failing tests which prevent generation of coverage docs. This is done by adding the command line argument CMD_ARGS= to the "make qgis_coverage" command. Eg, make qgis_coverage CMD_ARGS="-E \"PalLabeling|LocalServer|AppStartup\" will skip tests which match the listed expressions.
1 parent f8f7842 commit a435441

File tree

3 files changed

+212
-0
lines changed

3 files changed

+212
-0
lines changed
 

‎CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ SET (PEDANTIC TRUE CACHE BOOL "Determines if we should compile in pedantic mode.
126126
# whether unit tests should be build
127127
SET (ENABLE_TESTS TRUE CACHE BOOL "Build unit tests?")
128128

129+
# whether coverage tests should be performed
130+
SET (ENABLE_COVERAGE FALSE CACHE BOOL "Perform coverage tests?")
131+
132+
# whether coverage documentation should be generated
133+
SET (GENERATE_COVERAGE_DOCS FALSE CACHE BOOL "Generate coverage docs (requires lcov)?")
134+
129135
# hide this variable because building of python bindings might fail
130136
# if set to other directory than expected
131137
MARK_AS_ADVANCED(LIBRARY_OUTPUT_PATH)
@@ -399,6 +405,16 @@ IF(MSVC)
399405
ADD_DEFINITIONS(-DNOMINMAX)
400406
ENDIF(MSVC)
401407

408+
IF(ENABLE_COVERAGE)
409+
INCLUDE("cmake/modules/coverage/CodeCoverage.cmake")
410+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
411+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
412+
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
413+
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
414+
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
415+
SETUP_TARGET_FOR_COVERAGE(qgis_coverage ctest coverage)
416+
ENDIF(ENABLE_COVERAGE)
417+
402418
#############################################################
403419
# platform specific stuff
404420

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#
2+
# 2012-01-31, Lars Bilke
3+
# - Enable Code Coverage
4+
#
5+
# 2013-09-17, Joakim Söderberg
6+
# - Added support for Clang.
7+
# - Some additional usage instructions.
8+
#
9+
# USAGE:
10+
11+
# 0. (Mac only) If you use Xcode 5.1 make sure to patch geninfo as described here:
12+
# http://stackoverflow.com/a/22404544/80480
13+
#
14+
# 1. Copy this file into your cmake modules path.
15+
#
16+
# 2. Add the following line to your CMakeLists.txt:
17+
# INCLUDE(CodeCoverage)
18+
#
19+
# 3. Set compiler flags to turn off optimization and enable coverage:
20+
# SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
21+
# SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
22+
#
23+
# 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
24+
# which runs your test executable and produces a lcov code coverage report:
25+
# Example:
26+
# SETUP_TARGET_FOR_COVERAGE(
27+
# my_coverage_target # Name for custom target.
28+
# test_driver # Name of the test driver executable that runs the tests.
29+
# # NOTE! This should always have a ZERO as exit code
30+
# # otherwise the coverage generation will not complete.
31+
# coverage # Name of output directory.
32+
# )
33+
#
34+
# 4. Build a Debug build:
35+
# cmake -DCMAKE_BUILD_TYPE=Debug ..
36+
# make
37+
# make my_coverage_target
38+
#
39+
#
40+
41+
# Check prereqs
42+
FIND_PROGRAM( GCOV_PATH gcov )
43+
IF(GENERATE_COVERAGE_DOCS)
44+
FIND_PROGRAM( LCOV_PATH lcov )
45+
FIND_PROGRAM( GENHTML_PATH genhtml )
46+
ENDIF()
47+
FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
48+
49+
IF(NOT GCOV_PATH)
50+
MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
51+
ENDIF() # NOT GCOV_PATH
52+
53+
IF(NOT CMAKE_COMPILER_IS_GNUCXX)
54+
# Clang version 3.0.0 and greater now supports gcov as well.
55+
MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.")
56+
57+
IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
58+
MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
59+
ENDIF()
60+
ENDIF() # NOT CMAKE_COMPILER_IS_GNUCXX
61+
62+
SET(CMAKE_CXX_FLAGS_COVERAGE
63+
"-g -O0 --coverage -fprofile-arcs -ftest-coverage"
64+
CACHE STRING "Flags used by the C++ compiler during coverage builds."
65+
FORCE )
66+
SET(CMAKE_C_FLAGS_COVERAGE
67+
"-g -O0 --coverage -fprofile-arcs -ftest-coverage"
68+
CACHE STRING "Flags used by the C compiler during coverage builds."
69+
FORCE )
70+
SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
71+
""
72+
CACHE STRING "Flags used for linking binaries during coverage builds."
73+
FORCE )
74+
SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
75+
""
76+
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
77+
FORCE )
78+
MARK_AS_ADVANCED(
79+
CMAKE_CXX_FLAGS_COVERAGE
80+
CMAKE_C_FLAGS_COVERAGE
81+
CMAKE_EXE_LINKER_FLAGS_COVERAGE
82+
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
83+
84+
IF ( NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "Coverage"))
85+
MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" )
86+
ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
87+
88+
89+
# Param _targetname The name of new the custom make target
90+
# Param _testrunner The name of the target which runs the tests.
91+
# MUST return ZERO always, even on errors.
92+
# If not, no coverage report will be created!
93+
# Param _outputname lcov output is generated as _outputname.info
94+
# HTML report is generated in _outputname/index.html
95+
# Optional fourth parameter is passed as arguments to _testrunner
96+
# Pass them in list form, e.g.: "-j;2" for -j 2
97+
FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
98+
99+
IF(GENERATE_COVERAGE_DOCS AND NOT LCOV_PATH)
100+
MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
101+
ENDIF() # NOT LCOV_PATH
102+
103+
IF(GENERATE_COVERAGE_DOCS AND NOT GENHTML_PATH)
104+
MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
105+
ENDIF() # NOT GENHTML_PATH
106+
107+
# Setup target
108+
IF(GENERATE_COVERAGE_DOCS)
109+
ADD_CUSTOM_TARGET(${_targetname}
110+
111+
# Cleanup lcov
112+
${LCOV_PATH} --directory . --zerocounters
113+
114+
# Run tests
115+
COMMAND ${_testrunner} \${CMD_ARGS}
116+
117+
# Capturing lcov counters and generating report
118+
COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 --directory . --capture --output-file ${_outputname}.info
119+
COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 --remove ${_outputname}.info 'tests/*' '/usr/*' '*.cxx' '*.sip' 'sip_*' 'ui_*' --output-file ${_outputname}.info.cleaned
120+
COMMAND ${GENHTML_PATH} --branch-coverage --rc lcov_branch_coverage=1 -o ${_outputname} ${_outputname}.info.cleaned
121+
COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info.cleaned
122+
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
123+
124+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
125+
)
126+
# Show info where to find the report
127+
ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
128+
COMMAND ;
129+
COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
130+
)
131+
ELSE(GENERATE_COVERAGE_DOCS)
132+
ADD_CUSTOM_TARGET(${_targetname}
133+
# Run tests
134+
COMMAND ${_testrunner} \${CMD_ARGS}
135+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
136+
)
137+
ENDIF()
138+
139+
ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
140+
141+
# Param _targetname The name of new the custom make target
142+
# Param _testrunner The name of the target which runs the tests
143+
# Param _outputname cobertura output is generated as _outputname.xml
144+
# Optional fourth parameter is passed as arguments to _testrunner
145+
# Pass them in list form, e.g.: "-j;2" for -j 2
146+
FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)
147+
148+
IF(NOT PYTHON_EXECUTABLE)
149+
MESSAGE(FATAL_ERROR "Python not found! Aborting...")
150+
ENDIF() # NOT PYTHON_EXECUTABLE
151+
152+
IF(NOT GCOVR_PATH)
153+
MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
154+
ENDIF() # NOT GCOVR_PATH
155+
156+
ADD_CUSTOM_TARGET(${_targetname}
157+
158+
# Run tests
159+
${_testrunner} ${ARGV3}
160+
161+
# Running gcovr
162+
COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml
163+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
164+
COMMENT "Running gcovr to produce Cobertura code coverage report."
165+
)
166+
167+
# Show info where to find the report
168+
ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
169+
COMMAND ;
170+
COMMENT "Cobertura code coverage report saved in ${_outputname}.xml."
171+
)
172+
173+
ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Boost Software License - Version 1.0 - August 17th, 2003
2+
3+
Permission is hereby granted, free of charge, to any person or organization
4+
obtaining a copy of the software and accompanying documentation covered by
5+
this license (the "Software") to use, reproduce, display, distribute,
6+
execute, and transmit the Software, and to prepare derivative works of the
7+
Software, and to permit third-parties to whom the Software is furnished to
8+
do so, all subject to the following:
9+
10+
The copyright notices in the Software and this entire statement, including
11+
the above license grant, this restriction and the following disclaimer,
12+
must be included in all copies of the Software, in whole or in part, and
13+
all derivative works of the Software, unless such copies or derivative
14+
works are solely in the form of machine-executable object code generated by
15+
a source language processor.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20+
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21+
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)
Please sign in to comment.