Quick support for Google Testing.
The tool contains a CMake script for easy configuration. Provided the tool was added as a submodule under tools
, you should add something like this to the root CMakeLists.txt:
set(???_TESTING ON CACHE BOOL "Compile and/or run self-tests")
if (???_TESTING)
include( ${CMAKE_CURRENT_SOURCE_DIR}/tools/testing/googletest.cmake )
endif()
Then in each tested library/module the tests themselves should be created:
##################################################################
## TESTING
##################################################################
if (???_TESTING)
add_test_executable(???-test DATA_PATH data LIBRARIES lib???)
add_test(NAME some.name COMMAND ???-test --gtest_filter=??? --data_path=${DATA_DIR})
...
endif()
When properly configured, call the build system with the test
target, either
make -j`nproc` && make test
or
ninja -j`nproc` && ninja test
or
ninja -j`nproc` && ctest .
The add_test_executable
function takes the name of the executable target to create and, optionally, name of the subdirectory with test code, data directory within the test subdir and list of libraries to link against:
Name of the test target to create.
If present, will be used as a base for other options. Defaults to tests
. Will be appended to ${CMAKE_CURRENT_SOURCE_DIR}
. The tests will be taken by globbing ${TEST_SUBDIR}/*.cc
.
If present, will configure file default_data_path.in.hpp
as default_data_path.hpp
, using ${CMAKE_CURRENT_SOURCE_DIR}/${TEST_SUBDIR}/${DATA_PATH}
as the default data directory. It will also create ${DATA_DIR}
with the same value in the parent scope.
List of targets this executable should be linked against. Apart from this list, the resulting target will be linked against gtest
, gmock
and ${CMAKE_THREAD_LIBS_INIT}
.