Expected build-failure tests in CMake(CMake 中的预期构建失败测试)
问题描述
有时最好检查某些东西是否无法构建,例如:
Sometimes it's good to check that certain things fail to build, e.g.:
// Next line should fail to compile: can't convert const iterator to iterator.
my_new_container_type::iterator it = my_new_container_type::const_iterator();
是否可以将这些类型的东西合并到 CMake/CTest 中?我在 CMakeLists.txt
中寻找类似的东西:
Is it possible to incorporate these types of things into CMake/CTest? I'm looking for something like this in CMakeLists.txt
:
add_build_failure_executable(
test_iterator_conversion_build_failure
iterator_conversion_build_failure.cpp)
add_build_failure_test(
test_iterator_conversion_build_failure
test_iterator_conversion_build_failure)
(当然,据我所知,这些特定的 CMake 指令并不存在.)
(Of course, these specific CMake directives don't exist, to the best of my knowledge.)
推荐答案
您可以或多或少地按照您的描述执行此操作.您可以添加一个无法编译的目标,然后添加一个调用 cmake --build
的测试来尝试构建目标.剩下的就是将测试属性 WILL_FAIL
设置为 true.
You can do this more or less as you described. You can add a target which will fail to compile, then add a test which invokes cmake --build
to try to build the target. All that remains is to set the test property WILL_FAIL
to true.
因此,假设您将测试放在名为will_fail.cpp"的文件中,其中包含:
So, say you have your tests in a file named "will_fail.cpp" which contains:
#if defined TEST1
non-compiling code for test 1
#elif defined TEST2
non-compiling code for test 2
#endif
然后你可以在你的 CMakeLists.txt 中有如下内容:
Then you can have something like the following in your CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(Example)
include(CTest)
# Add a couple of failing-to-compile targets
add_executable(will_fail will_fail.cpp)
add_executable(will_fail_again will_fail.cpp)
# Avoid building these targets normally
set_target_properties(will_fail will_fail_again PROPERTIES
EXCLUDE_FROM_ALL TRUE
EXCLUDE_FROM_DEFAULT_BUILD TRUE)
# Provide a PP definition to target the appropriate part of
# "will_fail.cpp", or provide separate files per test.
target_compile_definitions(will_fail PRIVATE TEST1)
target_compile_definitions(will_fail_again PRIVATE TEST2)
# Add the tests. These invoke "cmake --build ..." which is a
# cross-platform way of building the given target.
add_test(NAME Test1
COMMAND ${CMAKE_COMMAND} --build . --target will_fail --config $<CONFIGURATION>
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME Test2
COMMAND ${CMAKE_COMMAND} --build . --target will_fail_again --config $<CONFIGURATION>
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
# Expect these tests to fail (i.e. cmake --build should return
# a non-zero value)
set_tests_properties(Test1 Test2 PROPERTIES WILL_FAIL TRUE)
如果你有很多这些要写的话,你显然可以将所有这些包装到一个函数或宏中.
You can obviously wrap all of this into a function or macro if you have a lot of these to write.
这篇关于CMake 中的预期构建失败测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CMake 中的预期构建失败测试
基础教程推荐
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01