1 # - Add tests using boost::test
3 # Add this line to your test files in place of including a basic boost test header:
4 # #include <BoostTestTargetConfig.h>
6 # If you cannot do that and must use the included form for a given test,
8 # // OVERRIDE_BOOST_TEST_INCLUDED_WARNING
9 # in the same file with the boost test include.
11 # include(BoostTestTargets)
12 # add_boost_test(<testdriver_name> SOURCES <source1> [<more sources...>]
13 # [FAIL_REGULAR_EXPRESSION <additional fail regex>]
14 # [LAUNCHER <generic launcher script>]
15 # [LIBRARIES <library> [<library>...]]
16 # [RESOURCES <resource> [<resource>...]]
17 # [TESTS <testcasename> [<testcasename>...]])
19 # If for some reason you need access to the executable target created,
20 # it can be found in ${${testdriver_name}_TARGET_NAME} as specified when
21 # you called add_boost_test
23 # Requires CMake 2.6 or newer (uses the 'function' command)
26 # GetForceIncludeDefinitions
27 # CopyResourcesToBuildTree
30 # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
31 # http://academic.cleardefinition.com
32 # Iowa State University HCI Graduate Program/VRAC
34 # Copyright Iowa State University 2009-2010.
35 # Distributed under the Boost Software License, Version 1.0.
36 # (See accompanying file LICENSE_1_0.txt or copy at
37 # http://www.boost.org/LICENSE_1_0.txt)
42 set(__add_boost_test YES)
44 set(BOOST_TEST_TARGET_PREFIX "test")
47 find_package(Boost 1.34.0 QUIET)
49 if("${Boost_VERSION}0" LESS "1034000")
51 "NOTE: boost::test-based targets and tests cannot "
52 "be added: boost >= 1.34.0 required but not found. "
53 "(found: '${Boost_VERSION}'; want >=103400) ")
57 "You may disable BUILD_TESTING to continue without the "
62 "BUILD_TESTING disabled, so continuing anyway.")
66 include(GetForceIncludeDefinitions)
67 include(CopyResourcesToBuildTree)
69 if(Boost_FOUND AND NOT "${Boost_VERSION}0" LESS "1034000")
70 set(_boosttesttargets_libs)
71 set(_boostConfig "BoostTestTargetsIncluded.h")
72 if(NOT Boost_UNIT_TEST_FRAMEWORK_LIBRARY)
73 find_package(Boost 1.34.0 QUIET COMPONENTS unit_test_framework)
75 if(Boost_UNIT_TEST_FRAMEWORK_LIBRARY)
76 set(_boosttesttargets_libs "${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}")
77 if(Boost_USE_STATIC_LIBS)
78 set(_boostConfig "BoostTestTargetsStatic.h")
81 set(_boostConfig "BoostTestTargetsDynamic.h")
85 get_filename_component(_moddir ${CMAKE_CURRENT_LIST_FILE} PATH)
86 configure_file("${_moddir}/${_boostConfig}"
87 "${CMAKE_CURRENT_BINARY_DIR}/BoostTestTargetConfig.h"
89 include_directories("${CMAKE_CURRENT_BINARY_DIR}")
92 function(add_boost_test _name)
99 set(_curdest _nowhere)
102 FAIL_REGULAR_EXPRESSION
108 USE_COMPILED_LIBRARY)
109 foreach(_arg ${_val_args} ${_bool_args})
112 foreach(_element ${ARGN})
113 list(FIND _val_args "${_element}" _val_arg_find)
114 list(FIND _bool_args "${_element}" _bool_arg_find)
115 if("${_val_arg_find}" GREATER "-1")
116 set(_curdest "${_element}")
117 elseif("${_bool_arg_find}" GREATER "-1")
118 set("${_element}" ON)
119 set(_curdest _nowhere)
121 list(APPEND ${_curdest} "${_element}")
126 message(FATAL_ERROR "Syntax error in use of add_boost_test!")
131 "Syntax error in use of add_boost_test: at least one source file required!")
134 if(Boost_FOUND AND NOT "${Boost_VERSION}0" LESS "1034000")
136 include_directories(${Boost_INCLUDE_DIRS})
139 foreach(src ${SOURCES})
140 file(READ ${src} thefile)
141 if("${thefile}" MATCHES ".*BoostTestTargetConfig.h.*")
142 set(includeType CONFIGURED)
143 set(includeFileLoc ${src})
145 elseif("${thefile}" MATCHES ".*boost/test/included/unit_test.hpp.*")
146 set(includeType INCLUDED)
147 set(includeFileLoc ${src})
148 set(_boosttesttargets_libs) # clear this out - linking would be a bad idea
152 ".*OVERRIDE_BOOST_TEST_INCLUDED_WARNING.*")
153 message("Please replace the include line in ${src} with this alternate include line instead:")
154 message(" \#include <BoostTestTargetConfig.h>")
155 message("Once you've saved your changes, re-run CMake. (See BoostTestTargets.cmake for more info)")
161 if(NOT _boostTestTargetsNagged${_name} STREQUAL "${includeType}")
162 if("includeType" STREQUAL "CONFIGURED")
164 "Test '${_name}' uses the CMake-configurable form of the boost test framework - congrats! (Including File: ${includeFileLoc})")
165 elseif("${includeType}" STREQUAL "INCLUDED")
166 message("In test '${_name}': ${includeFileLoc} uses the 'included' form of the boost unit test framework.")
168 message("In test '${_name}': Didn't detect the CMake-configurable boost test include.")
169 message("Please replace your existing boost test include in that test with the following:")
170 message(" \#include <BoostTestTargetConfig.h>")
171 message("Once you've saved your changes, re-run CMake. (See BoostTestTargets.cmake for more info)")
174 set(_boostTestTargetsNagged${_name}
183 list(APPEND SOURCES ${RESOURCES})
186 # Generate a unique target name, using the relative binary dir
187 # and provided name. (transform all / into _ and remove all other
188 # non-alphabet characters)
191 "${CMAKE_BINARY_DIR}"
192 "${CMAKE_CURRENT_BINARY_DIR}")
193 string(REGEX REPLACE "[^A-Za-z/_]" "" targetpath "${targetpath}")
194 string(REPLACE "/" "_" targetpath "${targetpath}")
196 set(_target_name ${BOOST_TEST_TARGET_PREFIX}-${targetpath}-${_name})
197 set(${_name}_TARGET_NAME "${_target_name}" PARENT_SCOPE)
200 add_executable(${_target_name} ${SOURCES})
202 list(APPEND LIBRARIES ${_boosttesttargets_libs})
205 target_link_libraries(${_target_name} ${LIBRARIES})
209 set_property(TARGET ${_target_name} PROPERTY RESOURCE ${RESOURCES})
210 copy_resources_to_build_tree(${_target_name})
213 if(NOT Boost_TEST_FLAGS)
214 # set(Boost_TEST_FLAGS --catch_system_error=yes --output_format=XML)
215 set(Boost_TEST_FLAGS --catch_system_error=yes)
218 # TODO: Figure out why only recent boost handles individual test running properly
221 set(_test_command ${LAUNCHER} "\$<TARGET_FILE:${_target_name}>")
223 set(_test_command ${_target_name})
226 if(TESTS AND ( "${Boost_VERSION}" VERSION_GREATER "103799" ))
227 foreach(_test ${TESTS})
230 ${_test_command} --run_test=${_test} ${Boost_TEST_FLAGS}
232 if(FAIL_REGULAR_EXPRESSION)
233 set_tests_properties(${_name}-${_test}
235 FAIL_REGULAR_EXPRESSION
236 "${FAIL_REGULAR_EXPRESSION}")
242 ${_test_command} ${Boost_TEST_FLAGS}
244 if(FAIL_REGULAR_EXPRESSION)
245 set_tests_properties(${_name}-boost_test
247 FAIL_REGULAR_EXPRESSION
248 "${FAIL_REGULAR_EXPRESSION}")
252 # CppCheck the test if we can.
253 if(COMMAND add_cppcheck)
254 add_cppcheck(${_target_name} STYLE UNUSED_FUNCTIONS)