googletest:如果测试失败,则执行其他操作

时间:2014-02-19 21:28:40

标签: c++ unit-testing debugging opencv googletest

我希望能够在测试失败的情况下将数据保存到磁盘。有没有办法在googletest框架内做到这一点?

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 个答案:

答案 0 :(得分:6)

Test::HasFailure()Test::HasNonfatalFailure()Test::HasFatalFailure()函数,如果(致命/非致命)失败,则返回true。你可以用它们来检查。

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
  }
}

请参阅 https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#checking-for-failures-in-the-current-test了解详情。