在gtest中如何期望程序退出? [英] How to expect program exit in gtest?

查看:371
本文介绍了在gtest中如何期望程序退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试使用 glog ,我想测试在某些情况下此检查失败.我的代码如下:

I'm testing some code that uses CHECK from glog and I'd like to test that this check fails in certain scenarios. My code looks like:

void MyClass::foo() {
  // stuff...
  // It's actually important that the binary gets aborted if this flag is false
  CHECK(some_flag) << "flag must be true";

  // more stuff...
}

我已经对gtest进行了一些研究,以及如何进行测试.我找到了EXPECT_FATAL_FALIUREEXPECT_NONFATAL_FAILUREHAS_FATAL_FAILURE,但是我还没有弄清楚如何使用它们.我相当有信心,如果我将CHECK(some_flag)更改为EXPECT_TRUE(some_flag),那么EXPECT_FATAL_FAILURE将可以正常工作,但是随后我在非测试文件中引入了测试依赖项,这很棘手.

I've done some research into gtest and how I might be able to test for this. I found EXPECT_FATAL_FALIURE, EXPECT_NONFATAL_FAILURE, and HAS_FATAL_FAILURE but I haven't managed to figure out how to use them. I'm fairly confident that if I change CHECK(some_flag) to EXPECT_TRUE(some_flag) then EXPECT_FATAL_FAILURE will work correctly but then I'm introducing test dependencies in non-test files which is...icky.

gtest是否有办法捕捉异常终止信号(或任何CHECK发出的信号)并期望它?

Is there a way for gtest to catch the abort signal (or whatever CHECK raises) and expect it?

推荐答案

aaa,我在发布此问题5分钟后找到了答案.典型的.

aaaand I found an answer 5 minutes after posting this question. Typical.

可以使用gtest中的死亡测试来完成.这是我的测试的样子:

This can be done using Death tests from gtest. Here's how my test looks:

TEST(MyClassTest, foo_death_test) {
  MyClass clazz(false); // make some_flag false so the CHECK fails
  ASSERT_DEATH( { clazz.foo(); }, "must be true");
}

通过.呜呼!

这篇关于在gtest中如何期望程序退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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