Valgrind失败不会失败CTest

时间:2018-11-04 13:21:58

标签: c++ cmake valgrind

我正在为我的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输出文件进行正则表达式似乎不文明和野蛮,我想避免这种情况。

1 个答案:

答案 0 :(得分:0)

基于上面的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

额外的findcat位是因为--output-on-failure仅显示测试输出,而不显示valgrind输出。