使用CMake/CTest仅运行更改或失败的测试
Running only changed or failed tests with CMake/CTest?
我在一个大型代码库中工作,该代码库有近400个测试可执行文件,运行时间在0.001秒到1800秒之间。当某些代码发生更改时,CMake将只智能地重建已更改的目标,所需时间将比实际测试运行所需时间短很多倍。
我知道的唯一方法是手动筛选您知道要运行的测试。我的直觉是,我想重新运行任何没有成功运行存储的测试套件——要么是因为它失败了,要么是因为重新编译了。
这可能吗?如果是,如何?
至于根据更改的可执行文件为这些选项生成值,您可以编写程序或脚本,检查可执行文件的修改时间和/或解析最后一个日志文件以查找失败的测试。
只运行已更改的可执行文件的另一种方法是将测试封装到附加脚本中。只有满足某些条件时,此脚本才会运行可执行文件。
对于Linux,包装器脚本可以实现如下:
test_wrapper.sh:
# test_wrapper.sh <test_name> <executable> <params..>
# Run executable, given as second argument, with parameters, given as futher arguments.
#
# If environment variable `LAST_LOG_FILE` is set,
# checks that this file is older than the executable.
#
# If environment variable LAST_LOG_FAILED_FILE is set,
# check that testname is listed in this file.
#
# Test executable is run only if one of these checks succeed, or if none of checks is performed.
check_succeed=
check_performed=
if [ -n $LAST_LOG_FILE ]; then
check_performed=1
executable=$2
if [ ! ( -e "$LAST_LOG_FILE" ) ]; then
check_succeed=1 # Log file is absent
elif [ "$LAST_LOG_FILE" -ot "$executable" ]; then
check_succeed=1 # Log file is older than executable
fi
fi
if [ -n "$LAST_LOG_FAILED_FILE" ]; then
check_performed=1
testname=$1
if [ ! ( -e "$LAST_LOG_FAILED_FILE" ) ]; then
# No failed tests at all
elif grep ":${testname}$" "$LAST_LOG_FAILED_FILE" > /dev/null; then
check_succeed=1 # Test has been failed previously
fi
fi
if [ -n "$check_performed" -a -z "$check_succeed" ]; then
echo "Needn't to run test."
exit 0
fi
shift 1 # remove `testname` argument
eval "$*"
用于添加包装测试的CMake宏:
CMakeLists.txt:
# Similar to add_test(), but test is executed with our wrapper.
function(add_wrapped_test name command)
if(name STREQUAL "NAME")
# Complex add_test() command flow: NAME <name> COMMAND <command> ...
set(other_params ${ARGN})
list(REMOVE_AT other_params 0) # COMMAND keyword
# Actual `command` argument
list(GET other_params 0 real_command)
list(REMOVE_AT other_params 0)
# If `real_command` is a target, need to translate it to path to executable.
if(TARGET real_command)
# Generator expression is perfectly OK here.
set(real_command "$<TARGET_FILE:${real_command}")
endif()
# `command` is actually value of 'NAME' parameter
add_test("NAME" ${command} "COMMAND" /bin/sh <...>/test_wrapper.sh
${command} ${real_command} ${other_params}
)
else() # Simple add_test() command flow
add_test(${name} /bin/sh <...>/test_wrapper.sh
${name} ${command} ${ARGN}
)
endif()
endfunction(add_wrapped_test)
当您只想运行那些测试时,哪些可执行文件自上次运行以来已更改或上次失败,请使用
LAST_LOG_FILE=<build-dir>/Testing/Temporary/LastTest.log
LAST_FAILED_LOG_FILE=<build-dir>/Testing/Temporary/LastTestsFailed.log
ctest
所有其他测试将自动通过。
相关文章:
- 从父数组测试用例构造二叉树失败
- 换位表导致测试失败(但在游戏中运行良好)
- 提升 1.64 单元测试编译失败
- 单元测试欧拉到四元数实现失败
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- G++10 的功能测试在 __cpp_lib_span 上失败
- 在 CppUnit 中测试中止断言失败
- 如何让谷歌测试正常运行。测试总是失败。(它不会编译)
- MS 本机单元测试 - 断言::线程失败不起作用
- 如果所有部分都失败,Catch2 将再次运行测试
- 如果出现警告,如何立即使自动测试失败
- 为什么在谷歌测试中调用"mkdtemp()"时失败
- 模型测试 + 简单表模式 = 父测试失败
- 用于查找连续子数组的最大总和的代码使测试用例失败
- 谷歌测试读写同一文件失败
- 测试用例状态为失败,但返回的值为 true
- kill syscommand 在 Catch2 测试用例中失败
- C++编译器 g++.exe 无法编译简单的测试程序 - 确定 CXX 编译器是否工作失败
- 对于循环不递增或循环不失败测试
- 电子邮件标题显示,dkim=硬失败(测试模式)和dkim=永久错误(坏信号)