我正在努力为我的travis版本添加内存检查测试。
[通常,我使用ctest --verbose
运行测试。我得到的输出很好:
2: [ RUN ] ContainsNoneTest.GivenSomeOfTheValuesShouldReturnFalse
2: [ OK ] ContainsNoneTest.GivenSomeOfTheValuesShouldReturnFalse (0 ms)
2: [----------] 4 tests from ContainsNoneTest (0 ms total)
2:
2: [----------] Global test environment tear-down
2: [==========] 49 tests from 13 test cases ran. (1 ms total)
2: [ PASSED ] 49 tests.
2/2 Test #2: LangTest ......................... Passed 0.00 sec
100% tests passed, 0 tests failed out of 2
Total Test time (real) = 0.20 sec
现在,我想添加valgrind。我在其中一种方法中放了一个小泄漏,用ctest --verbose -T memcheck
运行测试:
2: [ RUN ] ContainsNoneTest.GivenSomeOfTheValuesShouldReturnFalse
2: [ OK ] ContainsNoneTest.GivenSomeOfTheValuesShouldReturnFalse (2 ms)
2: [----------] 4 tests from ContainsNoneTest (8 ms total)
2:
2: [----------] Global test environment tear-down
2: [==========] 49 tests from 13 test cases ran. (453 ms total)
2: [ PASSED ] 49 tests.
2/2 MemCheck #2: LangTest ......................... Passed 1.00 sec
2: process test output now: LangTest LangTest
PostProcessTest memcheck results for : LangTest
100% tests passed, 0 tests failed out of 2
Total Test time (real) = 6.56 sec
-- Processing memory checking output:
2/2 MemCheck: #2: LangTest ......................... Defects: 12
MemCheck log files can be found here: ( * corresponds to test number)
/home/rumcajs/CLionProjects/ModernCppChallenge/cmake-build-debug/Testing/Temporary/MemoryChecker.*.log
Memory checking results:
Memory Leak - 12
太棒了,valgrind检测到泄漏。不幸的是,该过程以返回代码0(用echo $?
检查)退出了。当检测到泄漏时,是否可以更改ctest
行为以输出非零返回码?对stdout或valgrind输出文件进行正则表达式似乎不文明且野蛮,我想避免这种情况。
基于上面的error-exitcode
注释,我在travis脚本中设置了以下内容。
首先,在配置CMake项目时添加error-exitcode
选项:
cmake -D MEMORYCHECK_COMMAND_OPTIONS="--error-exitcode=1 --leak-check=full" $SOURCE
然后进行测试,我运行:
if ! ctest -D ExperimentalMemCheck --output-on-failure; then
find Testing/Temporary -name "MemoryChecker.*.log" -exec cat {} +
exit 1
fi
额外的find
和cat
位是因为--output-on-failure
仅显示测试输出,而不显示valgrind输出。