Google C++ Testing Framework之断言

南宫勇军
2023-12-01

本节将会深入一步,了解 gTest 的断言机制。

开始。

1. 我们先从测试代码第一行开始:

TEST(IsPrimeTest, TrueCondition)

宏:

    TEST 是一个宏,有两个参数,googtest对其解释为:test_case_name , test_name

    EXPECT_TRUE,见字知义,用来比较所传递的参数是否返回 true 。类似的有EXPECT_FALSE。

另:googletest还包装了另一个宏 ASSERT_*, 与EXPECT_* 的区别是:

    ASSERT_*版本的断言失败时会产生致命失败,并结束当前函数,后面的语句将不会执行。

    EXPECT_*版本的断言产生非致命失败,不会中止当前函数,也就是说,案例继续往下执行。 

2. 常用断言如下:

<1>. 基本的true/false条件测试:

Fatal assertion Nonfatal assertion Verifies
ASSERT_TRUE(condition); EXPECT_TRUE(condition); condition is true
ASSERT_FALSE(condition); EXPECT_FALSE(condition); condition is false

<2>. 数值比较 测试:

Fatal assertion Nonfatal assertion Verifies
ASSERT_EQ(expected, actual);EXPECT_EQ(expected, actual);expected == actual
ASSERT_NE(val1, val2); EXPECT_NE(val1, val2); val1 != val2
ASSERT_LT(val1, val2); EXPECT_LT(val1, val2); val1 < val2
ASSERT_LE(val1, val2); EXPECT_LE(val1, val2); val1 <= val2
ASSERT_GT(val1, val2); EXPECT_GT(val1, val2); val1 > val2
ASSERT_GE(val1, val2); EXPECT_GE(val1, val2); val1 >= val2

<3>. 比较两个C字符串(即char*或wchar_t*)测试:

(如果需要比较两个字符串对象,则相应地使用EXPECT_EQ等等)

Fatal assertion Nonfatal assertion Verifies
ASSERT_STREQ(expected_str, actual_str); EXPECT_STREQ(expected_str, actual_str); the two C strings have the same content
ASSERT_STRNE(str1, str2); EXPECT_STRNE(str1, str2); the two C strings have different content
ASSERT_STRCASEEQ(expected_str, actual_str);EXPECT_STRCASEEQ(expected_str, actual_str); the two C strings have the same content, ignoring case
ASSERT_STRCASENE(str1, str2);EXPECT_STRCASENE(str1, str2); the two C strings have different content, ignoring case

<4>. 显示成功或失败(AdvanceGuide):

SUCCEED();
FAIL(); ADD_FAILURE(); ADD_FAILURE_AT("file_path", line_number);

<5>. 异常检查(AdvanceGuide):

Fatal assertion Nonfatal assertion Verifies
ASSERT_THROW(statement, exception_type); EXPECT_THROW(statement, exception_type); statement throws an exception of the given type
ASSERT_ANY_THROW(statement); EXPECT_ANY_THROW(statement); statement throws an exception of any type
ASSERT_NO_THROW(statement); EXPECT_NO_THROW(statement); statement doesn't throw any exception

<6>. 使用已有的布尔型函数:

Fatal assertion Nonfatal assertion Verifies
ASSERT_PRED1(pred1, val1); EXPECT_PRED1(pred1, val1); pred1(val1) returns true
ASSERT_PRED2(pred2, val1, val2); EXPECT_PRED2(pred2, val1, val2); pred2(val1, val2) returns true
... ... ...

<7>. 还可自定义输出格式:

Fatal assertion Nonfatal assertion Verifies
ASSERT_PRED_FORMAT1(pred_format1, val1); EXPECT_PRED_FORMAT1(pred_format1, val1`); pred_format1(val1) is successful
ASSERT_PRED_FORMAT2(pred_format2, val1, val2); EXPECT_PRED_FORMAT2(pred_format2, val1, val2); pred_format2(val1, val2) is successful
... ... ...

<8>. 浮点型检查:

Fatal assertion Nonfatal assertion Verifies
ASSERT_FLOAT_EQ(expected, actual); EXPECT_FLOAT_EQ(expected, actual); the two float values are almost equal
ASSERT_DOUBLE_EQ(expected, actual); EXPECT_DOUBLE_EQ(expected, actual); the two double values are almost equal

    两个相近数比较。

Fatal assertion Nonfatal assertion Verifies
ASSERT_NEAR(val1, val2, abs_error); EXPECT_NEAR(val1, val2, abs_error);

the difference between val1 and val2 doesn't exceed

the given absolute error

<9>. Windows HRESULT assertions

Fatal assertion Nonfatal assertion Verifies
ASSERT_HRESULT_SUCCEEDED(expression); EXPECT_HRESULT_SUCCEEDED(expression); expression is a success HRESULT
ASSERT_HRESULT_FAILED(expression); EXPECT_HRESULT_FAILED(expression); expression is a failure HRESULT

总结:

通过以上常用断言总结,我们可以看到,gTest提供了非常丰富的断言来帮助我们更好地执行测试,编写更高效的自动化测试代码。

转载于:https://www.cnblogs.com/way_testlife/archive/2010/10/18/1854908.html

 类似资料: