【问题标题】:CMake and add test program commandCMake并添加测试程序命令
【发布时间】:2016-07-18 12:24:33
【问题描述】:

我通常使用 makefile 进行项目,但我想开始学习 CMake。 我使用 makefile 不仅用于构建我的项目,还用于测试我的项目。 它非常有用。 如何使用 CMake 做到这一点?

例如这个makefile:

pathword=words.txt
flags=-std=c++11 -Wall -Wextra -g -Og
#flags=-std=c++11 -O3 -DNDEBUG -s

default: TextMiningCompiler TextMiningApp

TextMiningCompiler: TextMiningCompiler.cpp trie.cpp
    g++ $(flags) TextMiningCompiler.cpp trie.cpp -o TextMiningCompiler

TextMiningApp: TextMiningApp.cpp
    g++ $(flags) TextMiningApp.cpp -o TextMiningApp

run: TextMiningCompiler TextMiningApp
    ./TextMiningCompiler $(pathword) totoro.txt
    cat test.txt | time ./TextMiningApp totoro.txt

clean:
    trash TextMiningCompiler TextMiningApp

我制作了这个 CMakefile:

cmake_minimum_required(VERSION 2.8.9)
project (TextMining)
add_executable(TextMiningApp TextMiningApp.cpp)
add_executable(TextMiningCompiler TextMiningCompiler.cpp trie.cpp read_words_file.cpp)
set_property(TARGET TextMiningApp PROPERTY CXX_STANDARD 11)
set_property(TARGET TextMiningCompiler PROPERTY CXX_STANDARD 11)

我怎样才能拥有 make run 功能?还是其他自定义函数?

【问题讨论】:

    标签: makefile cmake


    【解决方案1】:

    在 CMake 中进行测试时,我更喜欢使用 add_test()。它允许 - 除了调用类似 make test 来运行测试 - 例如通过(随 CMake 分发)获取测试报告。

    add_test() 中使用可执行文件的 CMake 目标名称作为“命令”直接将其替换为可执行文件的路径:

    cmake_minimum_required(VERSION 2.8.9)
    project (TextMining)
    
    enable_testing()
    
    set(CMAKE_CXX_STANDARD 11)
    set(pathword "${CMAKE_SOURCE_DIR}/words.txt")
    
    add_executable(TextMiningCompiler TextMiningCompiler.cpp trie.cpp read_words_file.cpp)
    add_test(
        NAME TestTextMiningCompiler 
        COMMAND TextMiningCompiler "${pathword}" "totoro.txt"
    )
    
    add_executable(TextMiningApp TextMiningApp.cpp)
    add_test(
        NAME TestTextMiningApp 
        COMMAND sh -c "cat ${CMAKE_SOURCE_DIR}/test.txt | time $<TARGET_FILE:TextMiningApp> totoro.txt"
    )
    set_tests_properties(TestTextMiningApp PROPERTIES DEPENDS TestTextMiningCompiler)
    

    如果您将命令行参数添加到TextMiningApp 以将test.txt 作为输入传递,您可以进一步消除对sh 等shell 的依赖:

    add_test(
        NAME TestTextMiningApp 
        COMMAND TextMiningApp -i "${CMAKE_SOURCE_DIR}/test.txt" "totoro.txt"
    )
    

    并且不需要添加time 调用,因为在通过make test 执行测试时会自动测量总执行时间(顺便说一句。相当于调用ctest):

    $ make test
    Running tests...
    Test project [... path to project's binary dir ...]
        Start 1: TestTextMiningCompiler
    1/2 Test #1: TestTextMiningCompiler ...........   Passed    0.11 sec
        Start 2: TestTextMiningApp
    2/2 Test #2: TestTextMiningApp ................   Passed    0.05 sec
    
    100% tests passed, 0 tests failed out of 2
    
    Total Test time (real) =   0.19 sec
    

    参考

    【讨论】:

      【解决方案2】:

      命令add_custom_target

      添加一个具有给定名称的目标来执行给定的命令。

      用法很简单:

      add_custom_target(run
           COMMAND ./TextMiningCompiler <pathword> totoro.txt
           COMMAND cat test.txt | time ./TextMiningApp totoro.txt
      )
      add_dependencies(run TextMiningCompiler TextMiningApp)
      

      【讨论】:

      • 也许只是复制/粘贴,但在示例代码 sn-p 中也不应该是 add_custom_target() 而不是 add_custom_command() 吗?
      • @Florian:是的,应该是add_custom_target,谢谢你的意见。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多