[英]How to catch an assert with Google test?
I'm programming some unit test with the Google test framework.我正在使用 Google 测试框架编写一些单元测试。 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?
有没有办法在 Google 测试中捕获断言?
Example code under test:测试中的示例代码:
int factorial(int n){
assert(n >= 0);
//....
}
And then the test:然后是测试:
#include <gtest/gtest.h>
TEST(FactorialTest,assertNegative){
EXPECT_ANY_THROW({
factorial(-1);
});
}
But EXPECT_ANY_THROW
doesn't catch the assert but only exceptions.但是
EXPECT_ANY_THROW
不会捕获断言,而只会捕获异常。 I'm searching for a solution to catch asserts.我正在寻找捕获断言的解决方案。
Google test provides ASSERT_DEATH
, EXPECT_DEATH
and other related macros .谷歌测试提供了
ASSERT_DEATH
、 EXPECT_DEATH
等相关宏。
This question and What are Google Test, Death Tests are each other's answers.这个问题和什么是谷歌测试,死亡测试是对方的答案。 Does that make them duplicates, or not?
这是否使它们重复? ;-)
;-)
EXPECT_FATAL_FAILURE(statement,text) and EXPECT_NONFATAL_FAILURE(statement,text) will only pass if 'statement' invokes a failing ASSERT_x or EXECT_x respectively. EXPECT_FATAL_FAILURE(statement,text) 和 EXPECT_NONFATAL_FAILURE(statement,text)只有在 'statement' 分别调用失败的ASSERT_x 或 EXECT_x 时才会通过。
These statements will pass in your tests:这些语句将通过您的测试:
EXPECT_NONFATAL_FAILURE( EXPECT_TRUE( 0 ), "" ); EXPECT_FATAL_FAILURE( ASSERT_TRUE( 0 ), "" );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.