如何使用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文件, 't work。
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 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 this function:
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)
b $ b
它调用 add_test
,并在列表中记住测试目标。通过此功能添加的项目中的每个测试。在根CMakeLists.txt中,我有以下代码:
it calls add_test
and remembers test target in a list. Every test in a project added by this function. 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}
)
它创建依赖于项目中所有单元测试的自定义目标。
It creates custom target that depends on all unit tests in a project. Custom command is runs after all_tests
target was built.
推荐答案
此表单已生成 all_tests
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屋!