CTest有多个命令 [英] CTest with multiple commands
问题描述
我正在使用CTest构建一些测试。通常,我可以通过简单的行来设置测试:
I'm building some tests using CTest. Usually, I can set up the test by simply the line:
ADD_TEST(Test_Name executable args)
然而,我遇到了一个问题,我有一些测试需要运行两个命令,是否有任何方式我可以在单个ctest中运行两个程序,或者我需要为每个程序创建一个新的测试?
However, I've run into a problem, I have some tests that require two commands to be run in order for it to work, is there any way I can run two programs within a single ctest, or am I required to create a new test for each?
谢谢。
推荐答案
add_test
命令只接受一个可执行文件,脚本。要以跨平台的方式做到这一点,请在CMake本身中编写脚本。当您运行 make
或 -P
选项用于运行任意大小的CMake脚本语言> make test ,而不是在Makefile生成时。
The add_test
command only accepts one executable, but you can run any executable that is really a script. To do this in a cross platform way, write the script in CMake itself. CMake has the -P
option for running arbitrary chunks of CMake scripting language when you run make
or make test
, rather than at Makefile generation time.
很遗憾,您无法将参数传递给这样的脚本。
Sadly you can't pass arguments to such a script. But you can set variables to values, which is just as good.
这个脚本可以调用 runtests.cmake
,它运行命令CMD1和CMD2,并检查每个非零返回代码,如果发生这种情况,将返回一个错误的CMake本身:
This script you can call runtests.cmake
, it runs the commands CMD1 and CMD2 and checks each for a non-zero return code, returning out of CMake itself with an error if that happens:
macro(EXEC_CHECK CMD)
execute_process(COMMAND ${CMD} RESULT_VARIABLE CMD_RESULT)
if(CMD_RESULT)
message(FATAL_ERROR "Error running ${CMD}")
endif()
endmacro()
exec_check(${CMD1})
exec_check(${CMD2})
...然后添加您的测试用例:
... and then add your test cases like so:
add_executable(test1 test1.c)
add_executable(test2 test2.c)
add_test(NAME test
COMMAND ${CMAKE_COMMAND}
-DCMD1=$<TARGET_FILE:test1>
-DCMD2=$<TARGET_FILE:test2>
-P ${CMAKE_CURRENT_SOURCE_DIR}/runtests.cmake)
b $ b
$< TARGET_FILE:test1>
在构建文件生成时扩展到可执行文件的完整路径。当你运行 make test
或等效的时候,这将运行cmake -P runtests.cmake,将CMD1和CMD2变量设置为合适的测试程序。然后脚本将按顺序执行您的2个程序。如果任一测试程序失败,整个测试失败。如果需要查看测试的输出,可以运行 make test ARGS = -V
$<TARGET_FILE:test1>
gets expanded to the full path to the executable at build-file generation time. When you run make test
or equivalent this will run "cmake -P runtests.cmake" setting the CMD1 and CMD2 variables to the appropriate test programs. The script will then execute your 2 programs in sequence. If either of the test programs fail, the whole test fails. If you need to see the output of the test, you can run make test ARGS=-V
这篇关于CTest有多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!