一 内容
-
请先阅读 CMake Tutorial4_1 安装目标 或更前文章。
-
主要说明 如何使用enable_testing和add_test命令测试。
-
文件目录
step4_2 |- MathFunctions |- CMakeLists.txt |- MathFunctions.h |- mysqrt.cc |- CMakeaLists.txt |- main.cc |- TutorialConfig.h.in
-
修改CMakeLists.txt
# 设置CMake最低版本 cmake_minimum_required(VERSION 3.16) # 设置项目名称及版本 project(Tutorial VERSION 1.0) # 生成可执行文件 add_executable(Tutorial main.cc) # 设置选项 option(USE_MYMATH "use Turotial math" ON) # 配置文件:拷贝文件到另一位置,并且修改其内容 configure_file(TutorialConfig.h.in TutorialConfig.h) if(USE_MYMATH) add_subdirectory(MathFunctions) list(APPEND extra_libs MathFunctions) endif() target_link_libraries(Tutorial PUBLIC ${extra_libs}) # 打印信息 message("PROJECT_BINARY_DIR=${PROJECT_BINARY_DIR}") # 增加include路径到目标,否则会无法include生成的TutorialConfig.h target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}") install(TARGETS Tutorial DESTINATION bin) install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" DESTINATION include) # ----add enable_testing() # does the application run add_test(NAME Runs COMMAND Tutorial 25) # does the usage message work? add_test(NAME Usage COMMAND Tutorial) set_tests_properties(Usage PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" ) # define a function to simplify adding tests function(do_test target arg result) add_test(NAME Comp${arg} COMMAND ${target} ${arg}) set_tests_properties(Comp${arg} PROPERTIES PASS_REGULAR_EXPRESSION ${result} ) endfunction(do_test) # do a bunch of result based tests do_test(Tutorial 4 "4 is 2") do_test(Tutorial 9 "9 is 3") do_test(Tutorial 5 "5 is 2.236") do_test(Tutorial 7 "7 is 2.645") do_test(Tutorial 25 "25 is 5") do_test(Tutorial -25 "-25 is [-nan|nan|0]") do_test(Tutorial 0.0001 "0.0001 is 0.01")
- 测试属性PASS_REGULAR_EXPRESSION用来验证输出中是否包含指定字符串。
-
修改main.cc
// 使用官方例子 // A simple program that computes the square root of a number #include <cmath> #include <iostream> #include <string> #include "TutorialConfig.h" // should we include the MathFunctions header? #ifdef USE_MYMATH # include "MathFunctions.h" #endif int main(int argc, char* argv[]) { if (argc < 2) { // report version std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." << Tutorial_VERSION_MINOR << std::endl; std::cout << "Usage: " << argv[0] << " number" << std::endl; return 1; } // convert input to double const double inputValue = std::stod(argv[1]); // which square root function should we use? #ifdef USE_MYMATH const double outputValue = mysqrt(inputValue); #else const double outputValue = sqrt(inputValue); #endif std::cout << "The square root of " << inputValue << " is " << outputValue << std::endl; return 0; }
二 构建及测试
lee@leedeMacBook-Pro step4_2 % cd build
lee@leedeMacBook-Pro build % cmake ..
-- The C compiler identification is AppleClang 12.0.0.12000032
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
PROJECT_BINARY_DIR=/Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build
lee@leedeMacBook-Pro build % ls
CMakeCache.txt CTestTestfile.cmake MathFunctions cmake_install.cmake
CMakeFiles Makefile TutorialConfig.h
lee@leedeMacBook-Pro build % cmake --build .
[ 25%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cc.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
[ 75%] Building CXX object CMakeFiles/Tutorial.dir/main.cc.o
[100%] Linking CXX executable Tutorial
[100%] Built target Tutorial
lee@leedeMacBook-Pro build % ls
CMakeCache.txt CTestTestfile.cmake MathFunctions TutorialConfig.h
CMakeFiles Makefile Tutorial cmake_install.cmake
- 如上,build下比之前的例子多了CTestTestfile.cmake
- 执行 ctest -N 和 ctest -VV ,结果如下:
lee@leedeMacBook-Pro build % ctest -N
Test project /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build
Test #1: Runs
Test #2: Usage
Test #3: Comp4
Test #4: Comp9
Test #5: Comp5
Test #6: Comp7
Test #7: Comp25
Test #8: Comp-25
Test #9: Comp0.0001
Total Tests: 9
lee@leedeMacBook-Pro build % ctest -VV
UpdateCTestConfiguration from :/Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/DartConfiguration.tcl
UpdateCTestConfiguration from :/Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/DartConfiguration.tcl
Test project /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build
Constructing a list of tests
Done constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
test 1
Start 1: Runs
1: Test command: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial "25"
1: Test timeout computed to be: 10000000
1: Computing sqrt of 25 to be 13
1: Computing sqrt of 25 to be 7.46154
1: Computing sqrt of 25 to be 5.40603
1: Computing sqrt of 25 to be 5.01525
1: Computing sqrt of 25 to be 5.00002
1: Computing sqrt of 25 to be 5
1: Computing sqrt of 25 to be 5
1: Computing sqrt of 25 to be 5
1: Computing sqrt of 25 to be 5
1: Computing sqrt of 25 to be 5
1: The square root of 25 is 5
1/9 Test #1: Runs ............................. Passed 0.47 sec
test 2
Start 2: Usage
2: Test command: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial
2: Test timeout computed to be: 10000000
2: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial Version 1.0
2: Usage: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial number
2/9 Test #2: Usage ............................ Passed 0.00 sec
test 3
Start 3: Comp4
3: Test command: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial "4"
3: Test timeout computed to be: 10000000
3: Computing sqrt of 4 to be 2.5
3: Computing sqrt of 4 to be 2.05
3: Computing sqrt of 4 to be 2.00061
3: Computing sqrt of 4 to be 2
3: Computing sqrt of 4 to be 2
3: Computing sqrt of 4 to be 2
3: Computing sqrt of 4 to be 2
3: Computing sqrt of 4 to be 2
3: Computing sqrt of 4 to be 2
3: Computing sqrt of 4 to be 2
3: The square root of 4 is 2
3/9 Test #3: Comp4 ............................ Passed 0.00 sec
test 4
Start 4: Comp9
4: Test command: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial "9"
4: Test timeout computed to be: 10000000
4: Computing sqrt of 9 to be 5
4: Computing sqrt of 9 to be 3.4
4: Computing sqrt of 9 to be 3.02353
4: Computing sqrt of 9 to be 3.00009
4: Computing sqrt of 9 to be 3
4: Computing sqrt of 9 to be 3
4: Computing sqrt of 9 to be 3
4: Computing sqrt of 9 to be 3
4: Computing sqrt of 9 to be 3
4: Computing sqrt of 9 to be 3
4: The square root of 9 is 3
4/9 Test #4: Comp9 ............................ Passed 0.00 sec
test 5
Start 5: Comp5
5: Test command: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial "5"
5: Test timeout computed to be: 10000000
5: Computing sqrt of 5 to be 3
5: Computing sqrt of 5 to be 2.33333
5: Computing sqrt of 5 to be 2.2381
5: Computing sqrt of 5 to be 2.23607
5: Computing sqrt of 5 to be 2.23607
5: Computing sqrt of 5 to be 2.23607
5: Computing sqrt of 5 to be 2.23607
5: Computing sqrt of 5 to be 2.23607
5: Computing sqrt of 5 to be 2.23607
5: Computing sqrt of 5 to be 2.23607
5: The square root of 5 is 2.23607
5/9 Test #5: Comp5 ............................ Passed 0.00 sec
test 6
Start 6: Comp7
6: Test command: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial "7"
6: Test timeout computed to be: 10000000
6: Computing sqrt of 7 to be 4
6: Computing sqrt of 7 to be 2.875
6: Computing sqrt of 7 to be 2.65489
6: Computing sqrt of 7 to be 2.64577
6: Computing sqrt of 7 to be 2.64575
6: Computing sqrt of 7 to be 2.64575
6: Computing sqrt of 7 to be 2.64575
6: Computing sqrt of 7 to be 2.64575
6: Computing sqrt of 7 to be 2.64575
6: Computing sqrt of 7 to be 2.64575
6: The square root of 7 is 2.64575
6/9 Test #6: Comp7 ............................ Passed 0.00 sec
test 7
Start 7: Comp25
7: Test command: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial "25"
7: Test timeout computed to be: 10000000
7: Computing sqrt of 25 to be 13
7: Computing sqrt of 25 to be 7.46154
7: Computing sqrt of 25 to be 5.40603
7: Computing sqrt of 25 to be 5.01525
7: Computing sqrt of 25 to be 5.00002
7: Computing sqrt of 25 to be 5
7: Computing sqrt of 25 to be 5
7: Computing sqrt of 25 to be 5
7: Computing sqrt of 25 to be 5
7: Computing sqrt of 25 to be 5
7: The square root of 25 is 5
7/9 Test #7: Comp25 ........................... Passed 0.00 sec
test 8
Start 8: Comp-25
8: Test command: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial "-25"
8: Test timeout computed to be: 10000000
8: The square root of -25 is 0
8/9 Test #8: Comp-25 .......................... Passed 0.00 sec
test 9
Start 9: Comp0.0001
9: Test command: /Users/lee/research/cmake_learn/cmake_tutorial/step4_2/build/Tutorial "0.0001"
9: Test timeout computed to be: 10000000
9: Computing sqrt of 0.0001 to be 0.50005
9: Computing sqrt of 0.0001 to be 0.250125
9: Computing sqrt of 0.0001 to be 0.125262
9: Computing sqrt of 0.0001 to be 0.0630304
9: Computing sqrt of 0.0001 to be 0.0323084
9: Computing sqrt of 0.0001 to be 0.0177018
9: Computing sqrt of 0.0001 to be 0.0116755
9: Computing sqrt of 0.0001 to be 0.0101202
9: Computing sqrt of 0.0001 to be 0.0100007
9: Computing sqrt of 0.0001 to be 0.01
9: The square root of 0.0001 is 0.01
9/9 Test #9: Comp0.0001 ....................... Passed 0.00 sec
100% tests passed, 0 tests failed out of 9
Total Test time (real) = 0.50 sec
三 Github
- 代码已上传Github