在CMake工程顶层目录内的CMakeLists.txt文件中添加最后一行,使得目录结构中包含test目录,并且编译完成时会在build目录下产生test_bin目录。
cmake_minimum_required(VERSION 2.8) project (your_project_name) add_subdirectory(src bin) add_subdirectory(test test_bin)
使用这个宏很简单,比如: TEST(result >= 66);
进入build目录编译,编译成功后,进入test_bin目录运行ctest,屏幕上会看到一些简要的结果信息: Test project /home/chenshu/work/CommonService/trunk/c++/PipeLine2/build/test_bin Start 1: similarity_test 1/1 Test #1: similarity_test ..................***Failed 0.31 sec 0% tests passed, 1 tests failed out of 1 Total Test time (real) = 0.89 sec The following tests FAILED: 1 - similarity_test (Failed) Errors while running CTest
并且会产生Testing/Temporary/目录,该目录下包含了几个文件,CTestCostData.txt LastTest.log LastTestsFailed.log,详细测试信息都在其中。
在test目录下的CMakeLists.txt文件中就像src目录下的一样配置,你需要什么库,就加在里面。不过需要一点下面的设置
include(CheckFunctionExists) include(CheckCXXSourceCompiles) include(CheckLibraryExists) include(CPack) enable_testing()
step3
test目录下创建一个main函数,里面编写测试代码,我尝试着使用boost的test框架,暂时还没有成功,因此使用了一个宏(来源于CppCMS的作者Artyom)
使用这个宏很简单,比如: TEST(result >= 66);
进入build目录编译,编译成功后,进入test_bin目录运行ctest,屏幕上会看到一些简要的结果信息: Test project /home/chenshu/work/CommonService/trunk/c++/PipeLine2/build/test_bin Start 1: similarity_test 1/1 Test #1: similarity_test ..................***Failed 0.31 sec 0% tests passed, 1 tests failed out of 1 Total Test time (real) = 0.89 sec The following tests FAILED: 1 - similarity_test (Failed) Errors while running CTest
并且会产生Testing/Temporary/目录,该目录下包含了几个文件,CTestCostData.txt LastTest.log LastTestsFailed.log,详细测试信息都在其中。