具有多个命令的 CTest

发布于 2024-09-06 10:20:34 字数 209 浏览 2 评论 0原文

我正在使用 CTest 构建一些测试。通常,我可以简单地通过以下行设置测试:

ADD_TEST(Test_Name executable args)

但是,我遇到了问题,我有一些测试需要运行两个命令才能使其工作,有什么方法可以运行两个程序在单个 ctest 中,或者我是否需要为每个 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)

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?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2024-09-13 10:20:34

add_test 命令仅接受一个可执行文件,但您可以运行任何实际上是脚本的可执行文件。要以跨平台方式执行此操作,请在 CMake 本身中编写脚本。 CMake 具有 -P 选项,用于在运行 makemake test 时运行 CMake 脚本语言的任意块,而不是在生成 Makefile 时运行。

遗憾的是,您无法将参数传递给这样的脚本。但是您可以将变量设置为值,这同样好。

您可以调用此脚本 runtests.cmake,它运行命令 CMD1 和 CMD2 并检查每个命令是否有非零返回代码,如果发生这种情况,则从 CMake 本身返回错误:

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})

...和然后添加您的测试用例,如下所示:

add_executable(test1 test1.c)
add_executable(test2 test2.c)
add_test(NAME test
    COMMAND ${CMAKE_COMMAND}
            -DCMD1=
lt;TARGET_FILE:test1>
            -DCMD2=
lt;TARGET_FILE:test2>
    -P ${CMAKE_CURRENT_SOURCE_DIR}/runtests.cmake)

$ 在构建文件生成时扩展到可执行文件的完整路径。当您运行 make test 或等效命令时,这将运行“cmake -P runtests.cmake”,将 CMD1 和 CMD2 变量设置为适当的测试程序。然后该脚本将按顺序执行您的 2 个程序。如果其中一个测试程序失败,则整个测试失败。如果需要查看测试的输出,可以运行 make test ARGS=-V

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.

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=
lt;TARGET_FILE:test1>
            -DCMD2=
lt;TARGET_FILE:test2>
    -P ${CMAKE_CURRENT_SOURCE_DIR}/runtests.cmake)

$<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

慈悲佛祖 2024-09-13 10:20:34

有一种简单但不是跨平台的方法可以实现这一点。

在 Linux 中,您可以使用 bash 执行多个命令:

add_test(
    NAME
        TestName
    COMMAND
        bash -c "COMMAND1 ; \
            COMMAND2 ; \
            ${CMAKE_CURRENT_BINARY_DIR}/testExecutable"
)

There is a simple, although not cross platform, way to achieve this.

In Linux you can use bash to execute multiple commands:

add_test(
    NAME
        TestName
    COMMAND
        bash -c "COMMAND1 ; \
            COMMAND2 ; \
            ${CMAKE_CURRENT_BINARY_DIR}/testExecutable"
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文