C/Cpp: Add CMake example
This commit is contained in:
parent
f5b8ed9334
commit
7a68766392
7 changed files with 201 additions and 14 deletions
62
cpp/test/CMakeLists.txt
Normal file
62
cpp/test/CMakeLists.txt
Normal file
|
@ -0,0 +1,62 @@
|
|||
include(GoogleTest)
|
||||
|
||||
## CONFIGURE TESTING LIBRARY
|
||||
|
||||
# download and add googletest testing library
|
||||
set(INSTALL_GTEST OFF CACHE BOOL "Install GTest")
|
||||
FetchContent_Declare(
|
||||
googletest
|
||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
GIT_TAG release-1.10.0
|
||||
)
|
||||
FetchContent_GetProperties(googletest)
|
||||
if(NOT googletest_POPULATED)
|
||||
FetchContent_Populate(googletest)
|
||||
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
|
||||
endif()
|
||||
|
||||
# convinience macro. Attention, the target must already exist
|
||||
macro(add_gtest TESTNAME)
|
||||
target_link_libraries(${TESTNAME} PUBLIC gtest gmock gtest_main)
|
||||
|
||||
if(GOOGLE_TEST_INDIVIDUAL)
|
||||
if(CMAKE_VERSION VERSION_LESS 3.10)
|
||||
gtest_add_tests(TARGET ${TESTNAME}
|
||||
TEST_PREFIX "${TESTNAME}."
|
||||
TEST_LIST TmpTestList)
|
||||
set_tests_properties(${TmpTestList} PROPERTIES FOLDER "Tests") # for IDEs
|
||||
else()
|
||||
gtest_discover_tests(${TESTNAME}
|
||||
TEST_PREFIX "${TESTNAME}."
|
||||
PROPERTIES FOLDER "Tests")
|
||||
endif()
|
||||
else()
|
||||
add_test(${TESTNAME} ${TESTNAME})
|
||||
set_target_properties(${TESTNAME} PROPERTIES FOLDER "Tests") # for IDEs
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
# add a make target with better test output, than the standard `make test`
|
||||
if(CMAKE_CONFIGURATION_TYPES)
|
||||
add_custom_target(check COMMAND env GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND}
|
||||
--force-new-ctest-process --output-on-failure
|
||||
--build-config "$<CONFIGURATION>")
|
||||
else()
|
||||
add_custom_target(check COMMAND env GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND}
|
||||
--force-new-ctest-process --output-on-failure)
|
||||
endif()
|
||||
set_target_properties(check PROPERTIES FOLDER "Scripts")
|
||||
|
||||
|
||||
## CONFIGURE TESTS
|
||||
|
||||
# # add tests for convinience interfaces
|
||||
# add_executable(interfacetests
|
||||
# main.cpp
|
||||
# template-test.cpp
|
||||
# )
|
||||
# add_gtest(interfacetests)
|
||||
|
||||
# target_include_directories(interfacetests INTERFACE ${gtest_SOURCE_DIR}/include)
|
||||
# target_link_libraries(interfacetests PRIVATE main_util_v2x)
|
||||
# target_link_libraries(interfacetests PRIVATE main_util_hmi)
|
16
cpp/test/main.cpp
Normal file
16
cpp/test/main.cpp
Normal file
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* @file test/main.cpp
|
||||
* @brief Main function for all unit tests.
|
||||
*
|
||||
* @author Name <email@example.com>
|
||||
* @copyright (c) Company Name/ Author, YEAR
|
||||
* @copyright Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>
|
||||
* @copyright Licensed under the GNU GPLv3 License <http://www.gnu.org/licenses/gpl-3.0.txt>
|
||||
*/
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue