c++ - CMake:如何指定 ctest 应在其中查找可执行文件的目录?

标签 c++ cmake googletest ctest

我想将 ctest 集成到一个 c++/c 项目中。我使用谷歌测试来编写单元测试。

我的 CMakeLists.txt 的相关部分如下所示:

...
####### CREATING EXE #######
add_executable(test_exe main.cpp test.cpp)
target_link_libraries(test_exe GTest::GTest GTest::Main)
set_target_properties (test_exe PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${UNIT_TEST_BIN_OUTPUT_DIR})
add_test(test_exe test_exe)

如您所见,我指定了可执行文件的输出目录 (UNIT_TEST_BIN_OUTPUT_DIR)。

当我使用终端时,可执行文件本身运行良好:

cd <UNIT_TEST_BIN_OUTPUT_DIR>
./test_exe

我想使用 ctest 来执行我的测试。所以我转到cmake生成的“ctest文件夹”。这里我想使用ctest来执行cmake中“add_test”添加的所有测试。

user@user:~/<dir to cmake>/cmake/unit_tests$ ctest
Test project /<dir to cmake>/cmake/unit_tests
    Start 1: test_exe
Could not find executable test_exe
Looked in the following places:
test_exe
test_exe
Release/test_exe
Release/test_exe
Debug/test_exe
Debug/test_exe
MinSizeRel/test_exe
MinSizeRel/test_exe
RelWithDebInfo/test_exe
RelWithDebInfo/test_exe
Deployment/test_exe
Deployment/test_exe
Development/test_exe
Development/test_exe
Unable to find executable: test_exe
1/1 Test #1: test_exe ......***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 - test_exe (Not Run)
Errors while running CTest

如果我将“test_exe”放在显示的路径之一中,它就可以正常工作。但我不希望他们在那里。

我的问题:

有没有办法告诉 ctest 它应该在 UNIT_TEST_BIN_OUTPUT_DIR 中查找以找到可执行文件?

最佳答案

Documentation for add_test 为命令的长格式 指定WORKING_DIRECTORY 选项。此选项的值用作测试运行的目录:

add_test(NAME test_exe COMMAND test_exe WORKING_DIRECTORY ${UNIT_TEST_BIN_OUTPUT_DIR})

如果只是想让测试找到可执行文件,使用就足够了

add_test(NAME test_exe COMMAND test_exe)

这是 add_test 命令的长格式。在这种形式下,CMake 检查 COMMAND 是否是一个目标 名称,如果是,则将其替换为对应于该目标的可执行文件的绝对路径。这样可以从任何目录运行测试。

请注意,目标的自动替换不适用于您使用的add_test缩写

关于c++ - CMake:如何指定 ctest 应在其中查找可执行文件的目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43981966/

相关文章:

c++ - WndProc 赋值问题

c++ - 如何使 AddressSanitizer 不检查第三方库

qt - 即使我设置了 CMAKE_PREFIX_PATH,Cmake 似乎也找不到所需的 Qt cmake 文件

c++ - 无法在谷歌测试中检查异常类型

c++ - 如何在 2012 年在 Linux 上设置 googletest?

c++ - 链接上 undefined symbol ___gxx_personality_v0

c++ - WAVE 文件的恐惧失败

c++ - map 给出一些模棱两可的值(value)

linux - CMake 使用库预加载每个测试

c++ - 谷歌测试和 Visual Studio 2010 RC