測試也是一個非常直接的步驟。在頂層CMakeList.txt文件最后,我們可以添加大量的基礎測試代碼,用來驗證應用程序是否正常工作。 include(CTest)# does the application run
add_test (TutorialRuns Tutorial 25)# does it sqrt of 25
add_test (TutorialComp25 Tutorial 25)
set_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")# does it handle negative numbers
add_test (TutorialNegative Tutorial -25)
set_tests_properties (TutorialNegative PROPERTIES PASS_REGULAR_EXPRESSION "-25 is 0")# does it handle small numbers
add_test (TutorialSmall Tutorial 0.0001)
set_tests_properties (TutorialSmall PROPERTIES PASS_REGULAR_EXPRESSION "0.0001 is 0.01")# does the usage message work?
add_test (TutorialUsage Tutorial)
set_tests_properties (TutorialUsage PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number")組建之后,就可以運行‘ctest’命令行工具運行這些測試代碼。 ‘PASS_REGULAR_EXPRESSION’測試應用程序輸出包含某些字符串。如果我們希望增加大量的測試代碼來測試不同的輸入值,我們可以考慮創建一個宏,示例如下: #define a macro to simplify adding tests, then use it
macro (do_test arg result)add_test (TutorialComp${arg} Tutorial ${arg})set_tests_properties (TutorialComp${arg}PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)# do a bunch of result based tests
do_test (25 "25 is 5")
do_test (-25 "-25 is 0")
4.調試與測試
~/CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (Tutorial)# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)# configure a header file to pass some of the CMake settings
# to the source code
configure_file ("${PROJECT_SOURCE_DIR}/TutorialConfig.h.in""${PROJECT_BINARY_DIR}/TutorialConfig.h")# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories ("${PROJECT_BINARY_DIR}")# add the MathFunctions library?
if (USE_MYMATH)include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")add_subdirectory (MathFunctions)set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif ()# add the executable
add_executable (Tutorial tutorial.c)
target_link_libraries (Tutorial ${EXTRA_LIBS})# add the install targets
install (TARGETS Tutorial DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"DESTINATION include)# enable testing
enable_testing ()# does the application run
add_test (TutorialRuns Tutorial 25)# does it sqrt of 25
add_test (TutorialComp25 Tutorial 25)
set_tests_properties (TutorialComp25PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")# does it handle negative numbers
add_test (TutorialNegative Tutorial -25)
set_tests_properties (TutorialNegativePROPERTIES PASS_REGULAR_EXPRESSION "-25 is 0")# does it handle small numbers
add_test (TutorialSmall Tutorial 0.0001)
set_tests_properties (TutorialSmallPROPERTIES PASS_REGULAR_EXPRESSION "0.0001 is 0.01")# does the usage message work?
add_test (TutorialUsage Tutorial)
set_tests_properties (TutorialUsagePROPERTIESPASS_REGULAR_EXPRESSION "Usage:.*number")
~/Tutorial.c
// A simple program that computes the square root of a number
#include "TutorialConfig.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>#ifdef USE_MYMATH
#include "MathFunctions.h"
#endifint main(int argc, char* argv[])
{double inputValue = atof(argv[1]); //根據編譯器的要求,變量要放在最前面double outputValue = 0;if (argc < 2) {fprintf(stdout, "%s Version %d.%d\n", argv[0], Tutorial_VERSION_MAJOR,Tutorial_VERSION_MINOR);fprintf(stdout, "Usage: %s number\n", argv[0]);return 1;}if (inputValue >= 0) {
#ifdef USE_MYMATHoutputValue = mysqrt(inputValue);
#elseoutputValue = sqrt(inputValue);
#endif}fprintf(stdout, "The square root of %g is %g\n", inputValue, outputValue);return 0;
}
~/TutorialConfig.h.in
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#cmakedefine USE_MYMATH