简单示例
如果想执行几个可执行的程序或是执行相同的可执行程序但添加不同的参数,可以使用CTest工具,CMake自带CTest和CPack。
cmake doc
- ctest.
- add_test.
- enable_testing.
- 添加两个可执行程序
目的:对两个不同的cpp文件生成不同的可执行文件。并同时执行ctest
CMakeLists.txt
#版本要求,如若没有此句代码,可能会发生错误
cmake_minimum_required(VERSION 3.4)
project(test)
add_executable(boo boo.cpp)
add_executable(foo foo.cpp)
#启用测试
enable_testing()
add_test(NAME boo COMMAND boo)
add_test(NAME foo COMMAND foo)
add_test(NAME foo-with-args COMMAND foo arg1 arg2 arg3)
# add_test(boo_usage boo)
# set_tests_properties(boo_usage PROPERTIES PASS_REGULAR_EXPRESSION "boo")
# add_test(foo_usage foo)
# set_tests_properties(foo_usage PROPERTIES PASS_REGULAR_EXPRESSION "foo argc:1")
# add_test(foo-with-args foo arg1 arg2 arg3)
# set_tests_properties(foo-with-args PROPERTIES PASS_REGULAR_EXPRESSION "foo argc:1")
boo.cpp
#include<iostream>intmain(int argc,char**argv){
std::cout<<"boo"<<std::endl;return0;}
foo.cpp
#include<iostream>intmain(int argc,char**argv){
std::cout<<"foo argc:"<<argc<<std::endl;for(int i=0;i<argc;i++){
std::cout<<"argv["<<i<<"]:"<<argv[i]<<std::endl;}return0;}
操作步骤:
- 执行 **cmake .**,生成预处理文件
- 执行 cmake --build ./,编译生成可执行文件
- 执行 ctest,测试执行。(修改为 ctest -C Debug 就会正常)此时会提示:
Start 1: booTest not available without configuration. (Missing "-C <config>"?)1/3 Test #1: boo ..............................***Not Run 0.00 sec Start 2: fooTest not available without configuration. (Missing "-C <config>"?)2/3 Test #2: foo ..............................***Not Run 0.00 sec Start 3: foo-with-argsTest not available without configuration. (Missing "-C <config>"?)3/3 Test #3: foo-with-args ....................***Not Run 0.00 sec0% tests passed, 3 tests failed out of 3Total Test time (real) = 0.05 secThe following tests FAILED: 1 - boo (Not Run) 2 - foo (Not Run) 3 - foo-with-args (Not Run)Errors while running CTest
在执行ctest时必须加上编译类型 build_config 参数 ,才可以正确执行。使用 ctest --help 可以查看帮助
输出测试的内容
默认情况下只有 Passed/Failed 提示信息,使用参数 -V/-VV 选项显示其他输出:ctest -V / ctest -VV
执行命令 ctest -V -C Debug
输出结果
test 1
Start 1: boo
1: Test command: .\cmakeTest32\Debug\boo.exe
1: Test timeout computed to be: 10000000
1: boo
1/3 Test #1: boo .............................. Passed 0.01 sec
test 2
Start 2: foo
2: Test command: .\cmakeTest32\Debug\foo.exe
2: Test timeout computed to be: 10000000
2: foo argc:1
2: argv[0]:./cmakeTest32/Debug/foo.exe
2/3 Test #2: foo .............................. Passed 0.01 sec
test 3
Start 3: foo-with-args
3: Test command: .\cmakeTest32\Debug\foo.exe "arg1" "arg2" "arg3"
3: Test timeout computed to be: 10000000
3: foo argc:4
3: argv[0]:./cmakeTest32/Debug/foo.exe
3: argv[1]:arg1
3: argv[2]:arg2
3: argv[3]:arg3
3/3 Test #3: foo-with-args .................... Passed 0.01 sec
100% tests passed, 0 tests failed out of 3
Total Test time (real) = 0.08 sec
测试子集
测试时可以不用运行的所有的测试,可以很简单的运行测试集合中的某一个子集。比如上个例子中只运行foo的测试。
使用命令: ctest -R testName
示例:
执行命令: ctest -VV -C Debug -R foo
输出:
test 2
Start 2: foo
2: Test command: .\cmakeTest32\Debug\foo.exe
2: Test timeout computed to be: 10000000
2: foo argc:1
2: argv[0]:./cmakeTest32/Debug/foo.exe
1/2 Test #2: foo .............................. Passed 0.01 sec
test 3
Start 3: foo-with-args
3: Test command: .\cmakeTest32\Debug\foo.exe "arg1" "arg2" "arg3"
3: Test timeout computed to be: 10000000
3: foo argc:4
3: argv[0]:./cmakeTest32/Debug/foo.exe
3: argv[1]:arg1
3: argv[2]:arg2
3: argv[3]:arg3
2/2 Test #3: foo-with-args .................... Passed 0.01 sec
The following tests passed:
foo
foo-with-args
100% tests passed, 0 tests failed out of 2
Total Test time (real) = 0.09 sec
结果只有foo的测试执行。
补充
add_test(NAME <name> COMMAND <command> [<arg>...]
[CONFIGURATIONS <config>...]
[WORKING_DIRECTORY <dir>]
[COMMAND_EXPAND_LISTS])
#eg
add_test(NAME mytest
COMMAND testDriver --config $<CONFIG>
--exe $<TARGET_FILE:myexe>)
上面的添加测试是书中介绍的例子。
CMake_Tutorial. 中使用另一种方式,目前而言,理解起来也比上面的清晰,更花哨的用法还需自己揣摩。
add_test(test_name test_function args)
set_tests_properties(test_name PROPERTIES PASS_REGULAR_EXPRESSION "测试结果与此处内容相等")
#eg
add_test(foo_usage foo arg1 arg2 arg3)
set_tests_properties(foo_usage PROPERTIES PASS_REGULAR_EXPRESSION "...")
#foo_usage 为测试名称,foo为add_execuable中的名称,后面arg1 arg2 arg3为参数。
#还可以将测试使用一个宏来定义,简化代码和工作量
#do_test:宏名 arg1 arg2:参数,可以自由更改数量 result:结果 func_name:execuable的名称
#不过这样也还是有一些局限性
macro(do_test arg1 arg2 result func_name)
add_test(test_${arg1}_${arg2} ${func_name} ${arg1} ${arg2})
set_tests_properties(test_${arg1}_${arg2} PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro(do_test)
版权归原作者 dxa572862121 所有, 如有侵权,请联系我们删除。