Skip to content

Commit ee76fbc

Browse files
committed
CMake: major improvements of unit tests and code converage
The collector doesn't depend on GoogleTest package provided by OS anymore. Instead it downloads predefined version directly from official GitHub page. Converage build mode has been replaced by new configuration option ENABLE_TESTS_COVERAGE.
1 parent 6521bfd commit ee76fbc

File tree

3 files changed

+83
-29
lines changed

3 files changed

+83
-29
lines changed

CMakeLists.txt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,23 @@ if (NOT COMPILER_SUPPORT_GNUXX11)
3939
endif()
4040

4141
# ------------------------------------------------------------------------------
42-
# Build options
43-
set(TESTS_DEFAULT OFF)
44-
4542
# Set default build type if not specified by user
46-
if (NOT CMAKE_BUILD_TYPE)
47-
set (CMAKE_BUILD_TYPE Release
48-
CACHE STRING "Choose type of build (Release/Debug/Coverage)." FORCE)
49-
endif()
50-
51-
string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE_UPPER)
52-
# Enable unit tests, by default, for coverage builds
53-
if (${BUILD_TYPE_UPPER} STREQUAL "COVERAGE")
54-
set(TESTS_DEFAULT ON)
43+
set(DEFAULT_BUILD_TYPE "Release")
44+
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
45+
# Use the default build type if not specified
46+
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.")
47+
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE
48+
STRING "Choose the type of build." FORCE)
49+
# Set the possible values of build type for cmake-gui
50+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
51+
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
5552
endif()
5653

5754
option(ENABLE_DOC_MANPAGE "Enable manual page building" ON)
5855
option(ENABLE_DOC_DOXYGEN "Enable code documentation building" OFF)
59-
option(ENABLE_TESTS "Build Unit tests (make test)" ${TESTS_DEFAULT})
56+
option(ENABLE_TESTS "Build Unit tests (make test)" OFF)
6057
option(ENABLE_TESTS_VALGRIND "Build Unit tests with Valgrind Memcheck" OFF)
58+
option(ENABLE_TESTS_COVERAGE "Enable support for code coverage" OFF)
6159
option(PACKAGE_BUILDER_RPM "Enable RPM package builder (make rpm)" OFF)
6260
option(PACKAGE_BUILDER_DEB "Enable DEB package builder (make deb)" OFF)
6361

@@ -68,11 +66,15 @@ option(PACKAGE_BUILDER_DEB "Enable DEB package builder (make deb)" OFF)
6866
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -std=gnu11")
6967
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
7068
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -Wall -Wextra -pedantic")
71-
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} --coverage -fprofile-arcs -ftest-coverage")
7269
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -std=gnu++11")
7370
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
7471
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wextra -pedantic")
75-
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} --coverage -fprofile-arcs -ftest-coverage")
72+
73+
if (ENABLE_TESTS AND ENABLE_TESTS_COVERAGE)
74+
# Additional flags for code coverage
75+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
76+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
77+
endif()
7678

7779
# ------------------------------------------------------------------------------
7880
# Project components
@@ -88,6 +90,7 @@ endif()
8890

8991
# ------------------------------------------------------------------------------
9092
# Status messages
93+
string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE_UPPER)
9194
MESSAGE(STATUS
9295
"\n\n"
9396
"IPFIXcol version.: ${IPFIXCOL_VERSION}\n"

CMakeModules/unit_tests.cmake

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
11
# ------------------------------------------------------------------------------
22
# Download GTest
33

4-
# GTest required threads support
4+
# Source: https://gist.github.com/ClintLiddick/51deffb768a7319e715071aa7bd3a3ab
5+
56
find_package(Threads REQUIRED)
6-
find_package(GTest 1.6.0 REQUIRED)
7-
include_directories(${GTEST_INCLUDE_DIRS})
7+
8+
include(ExternalProject)
9+
ExternalProject_Add(
10+
googletest
11+
GIT_REPOSITORY https://github.com/google/googletest.git
12+
GIT_TAG "release-1.8.1"
13+
UPDATE_COMMAND ""
14+
INSTALL_COMMAND ""
15+
LOG_DOWNLOAD ON
16+
LOG_CONFIGURE ON
17+
LOG_BUILD ON
18+
)
19+
20+
ExternalProject_Get_Property(googletest source_dir)
21+
set(GTEST_INCLUDE_DIRS ${source_dir}/googletest/include)
22+
set(GMOCK_INCLUDE_DIRS ${source_dir}/googlemock/include)
23+
24+
ExternalProject_Get_Property(googletest binary_dir)
25+
set(GTEST_LIBRARY_PATH ${binary_dir}/googlemock/gtest/${CMAKE_FIND_LIBRARY_PREFIXES}gtest.a)
26+
set(GTEST_LIBRARY gtest)
27+
add_library(${GTEST_LIBRARY} UNKNOWN IMPORTED)
28+
set_target_properties(${GTEST_LIBRARY} PROPERTIES
29+
IMPORTED_LOCATION ${GTEST_LIBRARY_PATH}
30+
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
31+
add_dependencies(${GTEST_LIBRARY} googletest)
32+
33+
set(GTEST_MAIN_LIBRARY_PATH ${binary_dir}/googlemock/gtest/${CMAKE_FIND_LIBRARY_PREFIXES}gtest_main.a)
34+
set(GTEST_MAIN_LIBRARY gtest_main)
35+
add_library(${GTEST_MAIN_LIBRARY} UNKNOWN IMPORTED)
36+
set_target_properties(${GTEST_MAIN_LIBRARY} PROPERTIES
37+
IMPORTED_LOCATION ${GTEST_MAIN_LIBRARY_PATH}
38+
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
39+
add_dependencies(${GTEST_MAIN_LIBRARY} googletest)
40+
41+
set(GMOCK_LIBRARY_PATH ${binary_dir}/googlemock/${CMAKE_FIND_LIBRARY_PREFIXES}gmock.a)
42+
set(GMOCK_LIBRARY gmock)
43+
add_library(${GMOCK_LIBRARY} UNKNOWN IMPORTED)
44+
set_target_properties(${GMOCK_LIBRARY} PROPERTIES
45+
IMPORTED_LOCATION ${GMOCK_LIBRARY_PATH}
46+
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
47+
add_dependencies(${GMOCK_LIBRARY} googletest)
48+
49+
set(GMOCK_MAIN_LIBRARY_PATH ${binary_dir}/googlemock/${CMAKE_FIND_LIBRARY_PREFIXES}gmock_main.a)
50+
set(GMOCK_MAIN_LIBRARY gmock_main)
51+
add_library(${GMOCK_MAIN_LIBRARY} UNKNOWN IMPORTED)
52+
set_target_properties(${GMOCK_MAIN_LIBRARY} PROPERTIES
53+
IMPORTED_LOCATION ${GMOCK_MAIN_LIBRARY_PATH}
54+
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
55+
add_dependencies(${GMOCK_MAIN_LIBRARY} ${GTEST_LIBRARY})
56+
57+
include_directories(
58+
${GTEST_INCLUDE_DIRS}
59+
${GMOCK_INCLUDE_DIRS}
60+
)
61+
62+
message (STATUS "Include dirs: " ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS})
863

964
# ------------------------------------------------------------------------------
1065
# Valgrind
@@ -40,7 +95,7 @@ function(unit_tests_register_test _file)
4095

4196
# Add executable
4297
add_executable(${CASE_NAME} ${ARGV})
43-
target_link_libraries(${CASE_NAME} PUBLIC ${GTEST_LIBRARIES} ipfixcol2base)
98+
target_link_libraries(${CASE_NAME} PUBLIC ${GTEST_LIBRARY} ${GMOCK_LIBRARY} ipfixcol2base)
4499

45100
# Add the test
46101
add_test(NAME ${CASE_NAME} COMMAND "$<TARGET_FILE:${CASE_NAME}>")
@@ -60,7 +115,7 @@ endfunction()
60115
function(unit_tests_register_target _target)
61116
# Get a test name
62117
set(CASE_NAME "test_${_target}")
63-
target_link_libraries(${_target} PUBLIC ${GTEST_LIBRARIES})
118+
target_link_libraries(${_target} PUBLIC ${GTEST_LIBRARY} ${GMOCK_LIBRARY})
64119

65120
# Add test(s)
66121
add_test(NAME ${CASE_NAME} COMMAND "$<TARGET_FILE:${_target}>")
@@ -111,7 +166,7 @@ function(coverage_add_target)
111166
COMMAND "${PATH_LCOV}" --directory . --zerocounters --quiet
112167

113168
# Run tests
114-
COMMAND "${CMAKE_CTEST_COMMAND}" --quiet
169+
COMMAND "${CMAKE_CTEST_COMMAND}"
115170

116171
# Capture the counters
117172
COMMAND "${PATH_LCOV}"
@@ -122,7 +177,7 @@ function(coverage_add_target)
122177
--output-file "${COVERAGE_FILE_RAW}"
123178
# Remove coverage of Google Test files, system headers, etc.
124179
COMMAND "${PATH_LCOV}"
125-
--remove "${COVERAGE_FILE_RAW}" 'gtest/*' 'tests/*' '/usr/*'
180+
--remove "${COVERAGE_FILE_RAW}" '${CMAKE_BINARY_DIR}/*' '${CMAKE_SOURCE_DIR}/tests/*' '/usr/*'
126181
--rc lcov_branch_coverage=1
127182
--quiet --output-file "${COVERAGE_FILE_CLEAN}"
128183
# Generate HTML report
@@ -139,8 +194,5 @@ function(coverage_add_target)
139194
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
140195
COMMENT "To see the coverage report, open ${COVERAGE_DIR}index.html"
141196
COMMAND ;
142-
)
143-
144-
endfunction()
145-
146-
197+
)
198+
endfunction()

tests/unit/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ add_subdirectory(core/netflow)
1717

1818
# Enable code coverage target (i.e. make coverage) when appropriate build
1919
# type is chosen.
20-
if (${BUILD_TYPE_UPPER} STREQUAL "COVERAGE")
20+
if (ENABLE_TESTS AND ENABLE_TESTS_COVERAGE)
2121
coverage_add_target()
2222
endif()
23-

0 commit comments

Comments
 (0)