Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

googletest: performing additional operation if test fail

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);
}
like image 501
memecs Avatar asked Feb 19 '14 21:02

memecs


1 Answers

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.

like image 184
hildensia Avatar answered Sep 22 '22 04:09

hildensia