c++ - cmake:使测试成功通过构建过程的一部分

标签 c++ cmake

我正在努力使测试的通过成为构建过程的一部分。

在这里,我使用 add_custom_command 将测试作为 POST_BUILD 步骤运行。

function(register_test NAME)

    add_test(${NAME} ${NAME})

    # make the test run as part of the build process
    add_custom_command(TARGET ${NAME} POST_BUILD COMMAND ${NAME})

endfunction()

这种方法的问题在于测试仅在构建目标时运行:

$ make
[ 50%] Built target lib1
Linking CXX executable ../../Debug/bin/lib1_test
Running 1 test case...
main.cpp(8): fatal error: in "lib1_test": 
    critical check lib1() == "lib1" has failed [error != lib1]

*** 1 failure is detected in the test module "Master Test Suite"

make[2]: *** [lib1/test/lib1_test] Error 201
make[1]: *** [lib1/test/CMakeFiles/lib1_test.dir/all] Error 2
make: *** [all] Error 2

如果不需要构建目标,则不运行测试,构建通过。

这里我没有做任何改动,只是重新运行构建过程

$ make
[ 50%] Built target lib1
[100%] Built target lib1_test

但是,如果 lib1_test 实际运行,则测试失败。

$ ./lib1/test/lib1_test 
Running 1 test case...
main.cpp(8): fatal error: in "lib1_test": 
    critical check lib1() == "lib1" has failed [error != lib1]

*** 1 failure is detected in the test module "Master Test Suite"

一个更好的方法是制作一个 lib1_test.passed 目标,它依赖于 lib1_test,运行测试,并且只有在测试通过时才创建。

我尝试过的:

我已经尝试使用 add_custom_target 创建一个依赖于 lib1_test 的目标 lib1_test.passed,如果成功,创建一个文件 lib1_test.passed:

add_custom_target(${NAME}.passed
    DEPENDS ${NAME}
    COMMAND ${NAME}
    COMMAND ${CMAKE_COMMAND} -E touch ${NAME}.passed)

我目前取得的成就有两个不足之处:

  • 测试的运行不是正常构建过程的一部分。
    也就是说,make 不会“构建” lib1_test.passed;
    我必须明确说明 make lib1_test.passed
  • make lib1_test.passed 将始终执行 lib1_test,无论 lib1_test.passed 是否比 lib1_test1 更新还是不是

问题:

如何让测试的运行成为构建的一部分,而失败的测试总是会重新运行?

最佳答案

这是我到目前为止所得到的。实现非常快速和肮脏,但它仍然有效。请检查并判断它是否满足您的需求。

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.12)

project(test)

enable_testing()

set(lib1_SRC lib.c)

add_library(lib1 ${lib1_SRC})

set(test_SRC test.c)

add_executable(libtest ${test_SRC})
target_link_libraries(libtest lib1)

add_test(NAME libtest COMMAND libtest)

add_custom_command(
  OUTPUT _libtest_completed
  COMMAND ctest -C $<CONFIGURATION> --output-on-failure
  COMMAND cmake -E touch _libtest_completed
  DEPENDS libtest
)

add_custom_target(
  libtest_force ALL
  DEPENDS _libtest_completed
)

为了完整起见,源文件:

lib.c:

#include "lib.h"

#include <time.h>

int lib_func() {
    return time(NULL) % 2;
}

库.h:

#pragma once

int lib_func();

测试.c:

#include "lib.h"

int main() {
    return lib_func();
}

不幸的是,由于 CMake bug,不可能直接依赖于 test 目标所以我们必须手动执行日落。

关于c++ - cmake:使测试成功通过构建过程的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36697186/

相关文章:

c++ - Qt信号发射和QThread执行流程

c++ - 使用构造函数初始化指向已定义结构的指针数组

c++ - 如何在 CLion 中启用 C++11?

c++ - 将代码移动到函数中时,错误 0xC0000138 在 DLL "myApp.exe"中找不到序号

c++ - 我怎样才能同时拥有转换构造函数和转换运算符?

c++ - 如何将代码签名算法的 OID 从 CRYPT_ALGORITHM_IDENTIFIER 转换为人类可读的字符串?

c++ - 驱动函数到底是什么?

makefile - 无法使用Cmake构建

visual-c++ - CMakeLists.txt 中的 CMake Warning (dev) : No cmake_minimum_required command is present. 一行代码如 cmake_minimum_required(VERSION 3.9)

c++ - CMake 未启用异常