googletest: performing additional operation if test fail

风格不统一 提交于 2019-12-12 10:55:39

问题


I'd like to be able to save data to disk in case the test fails. Is there any way to do it within the googletest framework?

TEST_F(test_similarity,are_similar) {

  ASSERT_GT(1e-10,norm(im0,im1));

  // If test fails save images to disk for comparison:
  imwrite("im0.png",im0);
  imwrite("im1.png",im1);
}

回答1:


There are the Test::HasFailure(), Test::HasNonfatalFailure() and Test::HasFatalFailure() functions, that return true if there was a (fatal/non-fatal) failure. You could use them to check.

TEST_F(test_similarity,are_similar) {

  EXPECT_GT(1e-10,norm(im0,im1)); // Note the change to EXPECT

  // If test fails save images to disk for comparison:
  if(HasFailure()) {  // if not in a TEST, use ::testing::Test::HasFailure()
    imwrite("im0.png",im0);
    imwrite("im1.png",im1);
    FAIL(); //We want to fail fatally; use ADD_FAILURE() to fail non-fatally
  }
}

See https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#checking-for-failures-in-the-current-test for details.



来源:https://stackoverflow.com/questions/21892841/googletest-performing-additional-operation-if-test-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!