使用多个命令进行 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 本身中编写脚本.CMake 具有 -P
选项,用于在运行 make
或 make test
时运行任意 CMake 脚本语言块,而不是在生成 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)
$<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屋!