googletest:如果测试失败,则执行其他操作 [英] googletest: performing additional operation if test fail

查看:142
本文介绍了googletest:如果测试失败,则执行其他操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果测试失败,我希望能够将数据保存到磁盘.在googletest框架内有什么方法可以做到这一点吗?

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

推荐答案

Test::HasFailure()Test::HasNonfatalFailure()Test::HasFatalFailure()函数,如果发生(致命/非致命)故障,则返回true .您可以使用它们进行检查.

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

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

这篇关于googletest:如果测试失败,则执行其他操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆