[CMake] CTest + unit test frameworks integration

Yuri Timenkov ytimenkov at parallels.com
Fri Jul 18 10:28:02 EDT 2008


Does anyone have an experience in integrating Unit test frameworks with CTest? AFAIK many of such frameworks provide means to register tests and test cases, and it would 
be nice to make ctest aware of such things, because it's not good to make changes in multiple places when I add new test.

I know, KDE has some things, but their approach doesn't allow to run single test. May be I'm wrong and it's complete ctest misusage...

I Just played with googletest (http://code.google.com/p/googletest) and came to such macro:

macro(ADD_UNIT_TESTS executable source)
	file(READ "${source}" contents)
	string(REGEX MATCHALL "TEST_?F?\\(([A-Za-z_ ,]+)\\)" found_tests ${contents})
	foreach(hit ${found_tests})
		string(REGEX REPLACE ".*\\(([A-Za-z_]+)[, ]*([A-Za-z_]+)\\).*" "\\1.\\2" test_name ${hit})
		add_test(${test_name} ${executable} --gtest_filter=${test_name})
	endforeach(hit)
endmacro(ADD_UNIT_TESTS)

This way you automatically propagate all tests and test-cases from sources to CTest.


More information about the CMake mailing list