如何使用 Google 测试捕获断言? [英] How to catch an assert with Google test?
问题描述
我正在使用 Google 测试框架编写一些单元测试.但是我想检查一些断言是否放置得很好并且有用.有没有办法在 Google 测试中捕获断言?
I'm programming some unit test with the Google test framework. But I want to check whether some asserts are well placed and are useful. Is there a way to catch an assert in Google test?
测试中的示例代码:
int factorial(int n){
assert(n >= 0);
//....
}
然后是测试:
#include <gtest/gtest.h>
TEST(FactorialTest,assertNegative){
EXPECT_ANY_THROW({
factorial(-1);
});
}
但是 EXPECT_ANY_THROW
不会捕获断言,而只会捕获异常.我正在寻找捕获断言的解决方案.
But EXPECT_ANY_THROW
doesn't catch the assert but only exceptions. I'm searching for a solution to catch asserts.
推荐答案
Google 测试提供 ASSERT_DEATH
、EXPECT_DEATH
等相关宏.
Google test provides ASSERT_DEATH
, EXPECT_DEATH
and other related macros.
这个问题和什么是谷歌测试,死亡测试是彼此的答案.这是否会使它们重复?;-)
This question and What are Google Test, Death Tests are each other's answers. Does that make them duplicates, or not? ;-)
这篇关于如何使用 Google 测试捕获断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!