使用 cmake 构建我的项目后如何运行 ctest [英] How to run ctest after building my project with cmake
问题描述
我希望每次成功构建我的项目时都启动我的测试.如果某些测试被破坏,我希望我的构建也被破坏.默认情况下,我需要通过运行 ctest
命令手动运行测试.CTest 实际上可以构建项目,但我使用调用 make
的 IDE 来构建源代码.而且 make
不运行测试.
I want my tests to be launched each time my project is successfully built. And if some tests are broken I want my build to be broken too. By default I need to run tests manually by running ctest
command. CTest can actually build project but I use IDE that invokes make
to build sources. And make
doesn't run tests.
我将此命令添加到我的根 CMakeLists.txt 文件中,但它不起作用.
I add this command to my root CMakeLists.txt file but it doesn't work.
add_custom_command(OUTPUT tests.txt
POST_BUILD
COMMAND ctest --output-on-failure)
CMake 不返回任何错误,一切正常,但我的自定义命令不调用.在 CMake 中每次成功构建后,如何运行某些内容?
CMake doesn't return any errors and everything builds fine but my custom command doesn't invokes. How can I run something after each successful build in CMake?
更新:
我的最终解决方案是创建这个宏:
My final solution is creating this macro:
macro(add_unit_test target target_test)
set(UNIT_TEST_TARGETS ${UNIT_TEST_TARGETS} ${target_test} PARENT_SCOPE)
add_test(target ${CMAKE_CURRENT_BINARY_DIR}/${target_test})
endmacro(add_unit_test)
它调用 add_test
并记住列表中的测试目标.此宏添加的项目中的每个测试.在根 CMakeLists.txt 我有这个代码:
It calls add_test
and remembers test target in a list. Every test in a project added by this macro. In the root CMakeLists.txt I have this code:
add_custom_target( all_tests ALL
DEPENDS ${UNIT_TEST_TARGETS}
)
add_custom_command(TARGET all_tests
COMMENT "Run tests"
POST_BUILD COMMAND ctest ARGS --output-on-failure
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
它创建依赖于项目中所有单元测试的自定义目标.自定义命令在 all_tests
目标构建后运行.
It creates custom target that depends on all unit tests in a project. Custom command is runs after all_tests
target was built.
推荐答案
这种形式的 add_custom_command
仅当另一个 CMake 目标依赖于tests.txt"时才会执行.我假设没有其他目标将tests.txt"作为输入文件,因此自定义命令永远不会运行.
This form of add_custom_command
will only execute if another CMake target has a dependency on "tests.txt". I assume that no other target has "tests.txt" as an input file, hence the custom command never runs.
我认为您可以使用 add_custom_command
的第二种形式来实现您的目标;类似:
I think you could use the second form of add_custom_command
to achieve your goal; something like:
add_custom_command(TARGET MainTest
POST_BUILD
COMMAND ctest -C $<CONFIGURATION> --output-on-failure)
这篇关于使用 cmake 构建我的项目后如何运行 ctest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!