在CMake工程顶层目录内的CMakeLists.txt文件中添加最后一行,使得目录结构中包含test目录,并且编译完成时会在build目录下产生test_bin目录。
使用这个宏很简单,比如:
进入build目录编译,编译成功后,进入test_bin目录运行ctest,屏幕上会看到一些简要的结果信息:
并且会产生Testing/Temporary/目录,该目录下包含了几个文件,CTestCostData.txt LastTest.log LastTestsFailed.log,详细测试信息都在其中。
cmake_minimum_required(VERSION 2.8)project (your_project_name)add_subdirectory(src bin)add_subdirectory(test test_bin)
在test目录下的CMakeLists.txt文件中就像src目录下的一样配置,你需要什么库,就加在里面。不过需要一点下面的设置
include(CheckFunctionExists)include(CheckCXXSourceCompiles)include(CheckLibraryExists)include(CPack)enable_testing()...add_test(name your_test command your_test)
step3
test目录下创建一个main函数,里面编写测试代码,我尝试着使用boost的test框架,暂时还没有成功,因此使用了一个宏(来源于CppCMS的作者Artyom)
///// // Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com> // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by// the Free Software Foundation, either version 3 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public License// along with this program. If not, see <http://www.gnu.org/licenses/>./////#ifndef CPPCMS_TEST_H#define CPPCMS_TEST_H#include <stdexcept>#include <sstream>#define TEST(X) \ do { \ if(X) break; \ std::ostringstream oss; \ oss << "Error " << __FILE__ << ":"<<__LINE__ << " "<<#X; \ throw std::runtime_error(oss.str()); \ }while(0) #endif
使用这个宏很简单,比如:
TEST(result >= 66);
进入build目录编译,编译成功后,进入test_bin目录运行ctest,屏幕上会看到一些简要的结果信息:
Test project /home/chenshu/work/CommonService/trunk/c++/PipeLine2/build/test_bin Start 1: similarity_test1/1 Test #1: similarity_test ..................***Failed 0.31 sec0% tests passed, 1 tests failed out of 1Total Test time (real) = 0.89 secThe following tests FAILED: 1 - similarity_test (Failed)Errors while running CTest
并且会产生Testing/Temporary/目录,该目录下包含了几个文件,CTestCostData.txt LastTest.log LastTestsFailed.log,详细测试信息都在其中。
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow