代码之家  ›  专栏  ›  技术社区  ›  claf

cmake&ctest:生成测试不生成测试

  •  79
  • claf  · 技术社区  · 17 年前

    我在cmake中尝试ctest,以便使用 make test 目标。问题是CMAKE不“理解”我愿意运行的测试必须被构建,因为它是项目的一部分。

    所以我正在寻找一种方法来显式地指定这种依赖关系。

    8 回复  |  直到 7 年前
        1
  •  64
  •   Flow Matt McDonald    9 年前

    它是 可以论证地 bug in CMake (以前跟踪过 here )这是不可能的。解决方法是执行以下操作:

    add_test(TestName ExeName)
    add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
                      DEPENDS ExeName)
    

    然后你就可以跑了 make check 它将编译并运行测试。如果你有几个测试,那么你必须使用 DEPENDS exe1 exe2 exe3 ... 在上面的行中。

        2
  •  47
  •   pynexj    8 年前

    实际上有一种方法可以使用 make test . 您需要将测试可执行文件的构建定义为测试之一,然后在测试之间添加依赖项。即:

    ADD_TEST(ctest_build_test_code
             "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target test_code)
    ADD_TEST(ctest_run_test_code test_code)
    SET_TESTS_PROPERTIES(ctest_run_test_code
                         PROPERTIES DEPENDS ctest_build_test_code)
    
        3
  •  11
  •   Trevor Robinson    13 年前

    我使用了Richq答案的变体。在顶层 CMakeLists.txt ,我添加了一个自定义目标, build_and_test ,用于构建和运行所有测试:

    find_package(GTest)
    if (GTEST_FOUND)
        enable_testing()
        add_custom_target(build_and_test ${CMAKE_CTEST_COMMAND} -V)
        add_subdirectory(test)
    endif()
    

    在各个子项目中 CMAKLISTS.TXT 文件下 test/ ,我添加每个测试可执行文件作为 BuffdIn and Syt检验 :

    include_directories(${CMAKE_SOURCE_DIR}/src/proj1)
    include_directories(${GTEST_INCLUDE_DIRS})
    add_executable(proj1_test proj1_test.cpp)
    target_link_libraries(proj1_test ${GTEST_BOTH_LIBRARIES} pthread)
    add_test(proj1_test proj1_test)
    add_dependencies(build_and_test proj1_test)
    

    用这种方法,我只需要 make build_and_test 而不是 make test (或) make all test 并且它只具有构建测试代码(及其依赖性)的好处。可惜我不能用目标的名字 test . 在我的例子中,这并不坏,因为我有一个顶级脚本,它通过调用 cmake 然后 make ,它可以翻译 测试 进入之内 BuffdIn and Syt检验 .

    显然,不需要GTest的东西。我只是碰巧使用/喜欢谷歌测试,并想与cmake/ctest分享一个使用它的完整示例。imho,这种方法还可以让我使用 ctest -V ,显示测试运行时的Google测试输出:

    1: Running main() from gtest_main.cc
    1: [==========] Running 1 test from 1 test case.
    1: [----------] Global test environment set-up.
    1: [----------] 1 test from proj1
    1: [ RUN      ] proj1.dummy
    1: [       OK ] proj1.dummy (0 ms)
    1: [----------] 1 test from proj1 (1 ms total)
    1:
    1: [----------] Global test environment tear-down
    1: [==========] 1 test from 1 test case ran. (1 ms total)
    1: [  PASSED  ] 1 test.
    1/2 Test #1: proj1_test .......................   Passed    0.03 sec
    
        4
  •  5
  •   Samuel    13 年前

    如果你想模仿 make check ,您可能会发现此wiki条目非常有用:

    http://www.cmake.org/Wiki/CMakeEmulateMakeCheck

    我刚刚检查过,这就是它所说的成功(cmake 2.8.10)。

        5
  •  4
  •   quant    11 年前

    省去头痛:

    make all test
    

    对我来说是开箱即用的,在运行测试之前将构建依赖项。考虑到这是多么简单,它几乎使本地人 make test 功能很方便,因为它允许您选择运行上次编译的测试,即使您的代码被破坏。

        6
  •  0
  •   Derrick    7 年前

    这就是我反复强调和使用的:

    set(${PROJECT_NAME}_TESTS a b c)
    
    enable_testing()
    add_custom_target(all_tests)
    foreach(test ${${PROJECT_NAME}_TESTS})
            add_executable(${test} EXCLUDE_FROM_ALL ${test}.cc)
            add_test(NAME ${test} COMMAND $<TARGET_FILE:${test}>)
            add_dependencies(all_tests ${test})
    endforeach(test)
    
    build_command(CTEST_CUSTOM_PRE_TEST TARGET all_tests)
    string(CONFIGURE \"@CTEST_CUSTOM_PRE_TEST@\" CTEST_CUSTOM_PRE_TEST_QUOTED ESCAPE_QUOTES)
    file(WRITE "${CMAKE_BINARY_DIR}/CTestCustom.cmake" "set(CTEST_CUSTOM_PRE_TEST ${CTEST_CUSTOM_PRE_TEST_QUOTED})" "\n")
    

    牛传染性胃肠炎病毒

        7
  •  -2
  •   Holmes Conan    13 年前

    以上答案都是完美的。但实际上,CMAKE使用CTEST作为其测试工具,因此执行该任务的标准方法(我认为是这样)是:

    enable_testing ()
    add_test (TestName TestCommand)
    add_test (TestName2 AnotherTestCommand)
    

    然后运行 克苏 制作 建立目标。在那之后,你可以跑 制作测试 或者只是运行

    ctest
    

    你会得到结果的。根据CMAKE 2.8进行测试。

    查看详细信息: http://cmake.org/Wiki/CMake/Testing_With_CTest#Simple_Testing

        8
  •  -4
  •   dyomas    13 年前

    所有的答案都是好的,但它们意味着违背了按命令运行测试的传统 make test . 我做过这个把戏:

    add_test(NAME <mytest>
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    COMMAND sh -c "make <mytarget>; $<TARGET_FILE:<mytarget>>")
    

    这意味着测试包括构建(可选)和运行可执行目标。

    推荐文章