现象:单独运行单元测试是成功的,但是ctest却告诉我失败了
原因:单元测试程序退出时没有返回 0。
过程:
我给 miniob 的github action上增加了单测,每次提交PR到main分支就会执行单测。
有次提交代码单测运行显示是通过的,但是github action中看到的结果总是失败的。在本地单独运行测试每次都是成功的,百思不得姐。后来执行 make test(或者ctest)可以重现github action的错误。百思不得姐的事情又变成看起来测试是成功的,但是ctest告诉我失败了,就像下面的结果:
miniob/build_release [83] $ ctest --rerun-failed --output-on-failure -C Debug
Test project /data/wangyunlai.wyl/project/github/miniob-me/build_release
Start 8: test_clog.test_clog
1/1 Test #8: test_clog.test_clog ..............***Failed 0.12 sec
Note: Google Test filter = test_clog.test_clog
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from test_clog
[ RUN ] test_clog.test_clog
[ OK ] test_clog.test_clog (4 ms)
[----------] 1 test from test_clog (4 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (4 ms total)
[ PASSED ] 1 test.
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.12 sec
The following tests FAILED:
8 - test_clog.test_clog (Failed)
Errors while running CTest
让我突然间醒悟的是,clog_test 这个测试程序执行完成时返回值不是0,在终端上运行 echo $?
可以看到,结果是8,突然又醒悟了。
我是在编译时增加了asan相关的选项,这个测试相关的代码有一些内存泄露,后来把内存泄露问题解决了,通过echo $?
查看返回值是0,再运行ctest就成功了。
miniob/build_release [83] $ ctest --rerun-failed --output-on-failure
Test project /data/wangyunlai.wyl/project/github/miniob-me/build_release
Start 8: test_clog.test_clog
1/1 Test #8: test_clog.test_clog .............. Passed 0.02 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.03 sec