Question

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);
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top