CTest integration

From CxxTest wiki

Jump to: navigation, search

CTest is a test suite runner usually combined with CMake, a build system.

There several ways to integrate with CTest, but basically you simply make a new executable for your unit tests and tell CTest to add that as a unit test. To make things easier for you, there are some macros for you. First of all, there's one on the CMake wiki.

There's also this macro your author wrote in five minutes:

macro(unit_test NAME CXX_FILE FILES)
	set(PATH_FILES "")
	foreach(part ${FILES})
		set(PATH_FILES "${CMAKE_CURRENT_SOURCE_DIR}/${part}" ${PATH_FILES})
	endforeach(part ${FILES})
	set(CXX_FILE_REAL "${CMAKE_CURRENT_SOURCE_DIR}/${CXX_FILE}")
	add_custom_command(
		OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${NAME}.cxx"
		COMMAND cxxtestgen.py --error-printer -o "${CMAKE_CURRENT_BINARY_DIR}/${NAME}.cxx" ${CXX_FILE_REAL}
		DEPENDS "${FILE}"
		)
	set(CXXTEST_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${NAME}.cxx")
	add_executable("${NAME}" "${CXXTEST_OUTPUT}" ${PATH_FILES})
	target_link_libraries("${NAME}" ${CXXTEST_LINK_LIBS})
	add_test("${NAME}" "${EXECUTABLE_OUTPUT_PATH}/${NAME}")
endmacro(unit_test)

It assumes cxxtestgen.py is in your PATH. If it isn't, you should modify the COMMAND to resemble something like what's in the CMake wiki. Usage of this macro is as follows:

#link against libm and libpthread
set(CXXTEST_LINK_LIBS m pthread)

#let cxxtest generate a cxx file based on parallel_math_test.hxx and link it against libraries in the above variable,
#and math.cxx and threads.cxx, which could be internal project files needed for the test
unit_test(parallel_math parallel_math_test.hxx math.cxx threads.cxx)

With both these added to a CMakeList.txt, cmake and ctest will do the rest for you.

Personal tools