testing - 如果更改了 CMAKE_RUNTIME_OUTPUT_DIRECTORY,则 CMake 无法找到测试

标签 testing cmake

我正在使用 CMake 构建我的项目,我正在尝试为每个模块创建一堆测试套件。显然,如果我修改变量 CMAKE_RUNTIME_OUTPUT_DIRECTORY,则 ctest 无法找到要运行的测试并失败。

我做了一个最小的例子来展示我在说什么,我在 Lubuntu 13.10 上使用 CMake 2.8.11.2 运行它。如果有人能告诉我这是否是错误和/或如何解决它,我将不胜感激。谢谢。

文件CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project (Test)

# Put all tests in the test directory, where the sources also are
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/test)

enable_testing()

add_subdirectory (${PROJECT_SOURCE_DIR}/test)

文件测试/CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

add_executable(ttest main.cpp)
add_test(ttest ttest)

文件测试/main.cpp:

int main() {
    return 0;
}

在新文件夹 build 中构建后,可执行文件会在文件夹 test 中正确创建。从构建中运行 make test 会产生以下输出:

Running tests...
Test project /home/svalorzen/Tests/cmake/build
    Start 1: ttest
Could not find executable ttest
Looked in the following places:
ttest
ttest
Release/ttest
Release/ttest
Debug/ttest
Debug/ttest
MinSizeRel/ttest
MinSizeRel/ttest
RelWithDebInfo/ttest
RelWithDebInfo/ttest
Deployment/ttest
Deployment/ttest
Development/ttest
Development/ttest
Unable to find executable: ttest
1/1 Test #1: ttest ............................***Not Run   0.00 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.00 sec

The following tests FAILED:
      1 - ttest (Not Run)
Errors while running CTest
make: *** [test] Error 8

最佳答案

编辑:

实际上我在 add_test 的文档中漏掉了一些东西:

If COMMAND specifies an executable target (created by add_executable) it will automatically be replaced by the location of the executable created at build time

所以使用 ttest而不是 $<TARGET_FILE:ttest>应该可以。


我遇到了同样的问题,但我真的不知道这是不是一个错误。

我找到的解决方案是在命令中提供测试可执行文件的路径(目标为 ttest):

在测试/CMakeLists.txt 中:

add_test(ttest test/ttest)

最后,您希望将路径存储在一个变量中,以便编写类似 ${test_dir}/ttest 的内容.

如果你想要更健壮的东西,最好是使用 long add_test命令和生成器表达式:

add_test(NAME ttest COMMAND $<TARGET_FILE:ttest>)

生成表达式$<TARGET_FILE:ttest>会扩展到可执行目标的输出文件(这里是ttest),所以你确定目录没有问题。 cmake documentation 中引用了其他表达式.

如果你有很多测试要声明,它会变得有点冗长,所以我使用像这样的宏:

macro (create_test target)
  add_test (NAME ${target} COMMAND $<TARGET_FILE:${target}>)
endmacro (create_test)

#[some code]

#test definition
create_test(ttest)

假设测试名称与可执行文件名称相同。

我找不到任何其他可行的解决方案。可以使用 add_test 设置工作目录,这显然使 ctest 找到了可执行文件,但随后测试因“BAD_COMMAND”错误而崩溃。

我是 cmake 的新手,所以可能还有其他解决方案。

关于testing - 如果更改了 CMAKE_RUNTIME_OUTPUT_DIRECTORY,则 CMake 无法找到测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21394768/

相关文章:

测试 BPEL 流程?

icons - 为 cmake-cpack 应用程序指定图标和菜单条目

cmake - 如何从我自己的同名查找模块中执行CMake的默认查找模块?

cmake - 如何让 cMake 仅对项目中的一部分目录使用 Debug 编译模式?

testing - 属性文件阅读器是否在 jmeter 非 gui 模式下工作

testing - 集成了测试工具的 Robotframework

javascript - 直接从 CasperJS 调用 JS 方法

ruby-on-rails - 如何测试使用 puntopagos 和 rest-client 的 Controller Action ?

build - 如何选择哪个 CMake 可执行目标将是默认目标?

cmake - add_custom_command 没有生成目标