add_executable(cpp_test test.cpp)target_link_libraries(cpp_test sum_integers)macro(add_catch_test _name _cost)math(EXPR num_macro_calls "${num_macro_calls} + 1")message(STATUS "add_catch_test called with ${ARGC} arguments: ${ARGV}")set(_argn "${ARGN}")if(_argn)message(STATUS "oops - macro received argument(s) we did not expect: ${ARGN}")endif()add_test(
NAME
${_name}
COMMAND
$<TARGET_FILE:cpp_test>[${_name}]--success --out
${PROJECT_BINARY_DIR}/tests/${_name}.log --durations yes
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR})set_tests_properties(
${_name}
PROPERTIES
COST ${_cost})endmacro()set(num_macro_calls 0)add_catch_test(short1.5)add_catch_test(long2.5 extra_argument)message(STATUS "in total there were ${num_macro_calls} calls to add_catch_test")
$ cmake .-- The CXX compiler identification is GNU 9.3.0-- Check for working CXX compiler:/usr/bin/c++-- Check for working CXX compiler:/usr/bin/c++-- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- add_catch_test called with 2 arguments:short;1.5-- add_catch_test called with 3 arguments:long;2.5;extra_argument
-- oops - macro received argument(s) we did not expect: extra_argument
-- in total there were 2 calls to add_catch_test
-- Configuring done
-- Generating done
-- Build files have been written to:/home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-07/recipe-01/cxx-example
$ make && make test
Running tests...
Test project /home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-07/recipe-01/cxx-example
Start 2:long1/2 Test #2:long............................. Passed 0.00 sec
Start 1:short2/2 Test #1:short............................ Passed 0.01 sec
100% tests passed,0 tests failed out of 2
Total Test time (real)=0.01 sec
color
# CMakeLists.txtcmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-02 LANGUAGES NONE)list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")include(colors)define_colors()message(STATUS "This is a normal message")message(STATUS "${Red}This is a red${ColourReset}")message(STATUS "${BoldRed}This is a bold red${ColourReset}")message(STATUS "${Green}This is a green${ColourReset}")message(STATUS "${BoldMagenta}This is bold${ColourReset}")# cmake/colors.cmake# colorize CMake output# code adapted from stackoverflow: http://stackoverflow.com/a/19578320# from post authored by https://stackoverflow.com/users/2556117/frasermacro(define_colors)if(WIN32)# has no effect on WIN32set(ColourReset "")set(ColourBold "")set(Red "")set(Green "")set(Yellow "")set(Blue "")set(Magenta "")set(Cyan "")set(White "")set(BoldRed "")set(BoldGreen "")set(BoldYellow "")set(BoldBlue "")set(BoldMagenta "")set(BoldCyan "")set(BoldWhite "")else()string(ASCII 27 Esc)set(ColourReset "${Esc}[m")set(ColourBold "${Esc}[1m")set(Red "${Esc}[31m")set(Green "${Esc}[32m")set(Yellow "${Esc}[33m")set(Blue "${Esc}[34m")set(Magenta "${Esc}[35m")set(Cyan "${Esc}[36m")set(White "${Esc}[37m")set(BoldRed "${Esc}[1;31m")set(BoldGreen "${Esc}[1;32m")set(BoldYellow "${Esc}[1;33m")set(BoldBlue "${Esc}[1;34m")set(BoldMagenta "${Esc}[1;35m")set(BoldCyan "${Esc}[1;36m")set(BoldWhite "${Esc}[1;37m")endif()endmacro()
$ cmake .# as below pic