GoogleTest初探(1)


此篇主要了解一下GoogleTest中的斷言。

總的來說,GoogleTest中的斷言分為兩大類:EXPECT_*和ASSERT_*,這兩者在測試成功或失敗后均會給出測試報告,區別是前者在測試失敗后會繼續執行下面的測試,而后者在測試失敗后會立即終止測試。

 GoogleTest中的比較斷言,涉及整型,字符串, 浮點型,布爾型的比較判斷

Fatal assertion Nonfatal assertion Verifies
ASSERT_TRUE(condition) EXPECT_TRUE(condition) condition == true
ASSERT_TRUE(condition) EXPECT_FALSE(condition) condition == false
ASSERT_EQ(expected, actual) EXPECT_EQ(expected, actual) expected == actual
ASSERT_NE(val1, val2) EXPECT_NE(val1, val2) val1 != val2
ASSERT_LE(val1, val2) EXPECT_LE(val1, val2) val1 <= val2
ASSERT_LT(val1, val2) EXPECT_LT(val1, val2) val1 < val2
ASSERT_GE(val1, val2) EXPECT_GE(val1, val2) val1 >= val2
ASSERT_GT(val1, val2) EXPECT_GT(val1, val2) val1 > val2
ASSERT_STREQ(expected, actual) EXPECT_STREQ(expected, actual) (C-string) expected == actual
ASSERT_STRNE(s1, s2) EXPECT_STRNE(s1, s2) (C-string) s1 != s2
ASSERT_STRCASEEQ(s1, s2) EXPECT_STRCASEEQ(s1, s2) (C-string) s1 == s2  ignoring case
ASSERT_STRCASENE(s1, s2) EXPECT_STRCASENE(s1, s2) (C-string) s1 != s2  ignoring case
ASSERT_FLOAT_EQ(expected, actual) EXPECT_FLOAT_EQ(expected, actual) (float) expected == actual
ASSERT_DOUBLE_EQ(expected, actual) EXPECT_DOUBLE_EQ(expected, actual) (double) expected == actual
ASSERT_NEAR(val1, val2, abs_error) EXPECT_NEAR(val1, val2, abs_error) abs(val1-val2) <= abs_error

 

異常斷言:

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

 

幾個特殊的斷言(可用於流程控制)

SUCCEED() 一個成功斷言
FAIL() 一個失敗斷言(fatal failure)
ADD_FAILURE() 一個失敗斷言(nonefatal failure)
ADD_FAILURE_AT(file, line) 在給定的文件的給定行數生成一個失敗斷言(nonefatal failure)

 

對於一些待測試的,返回值為bool變量的函數,可以使用以下的斷言(n代表參數個數):

Fatal assertion Nonfatal assertion Verifies
ASSERT_PREDn(predn, val1,...); EXPECT_PREDn(predn, val1,...); predn(val1,...) is true

另外,可以對測試函數進行一個包裝,使用以下斷言來判斷包裝好的函數:

Fatal assertion Nonefatal assertion Verifies
ASSERT_PRED_FORMATn(pred_formatn, val1, ...); EXPECT_PRED_FORMATn(pred_formatn, val1, ...); pred_formatn(val1, ...) is successful

要求pred_formatn的返回值是AssertionResult, 例子如下:

// Returns true if m and n have no common divisors except 1.
bool MutuallyPrime(int m, int n) { ... }

// Returns the smallest prime common divisor of m and n,
// or 1 when m and n are mutually prime.
int SmallestPrimeCommonDivisor(int m, int n) { ... }

// A predicate-formatter for asserting that two integers are mutually prime.
::testing::AssertionResult AssertMutuallyPrime(const char* m_expr,
                                               const char* n_expr,
                                               int m,
                                               int n) 
{
  if (MutuallyPrime(m, n)) return ::testing::AssertionSuccess();

  return ::testing::AssertionFailure() << m_expr << " and " << n_expr
      << " (" << m << " and " << n << ") are not mutually prime, "
      << "as they have a common divisor " << SmallestPrimeCommonDivisor(m, n);
}

使用一個AssertMutuallyPrime函數來包裝一下待測試函數MutuallyPrime。此時再使用斷言:

EXPECT_PRED_FORMAT2(AssertMutuallyPrime, b, c);

如果失敗,則會打印出失敗信息

b and c (4 and 10) are not mutually prime, as they have a common divisor 2.

還有類型斷言:

::testing::StaticAssertTypeEq<T1, T2>();

使用這個函數可以判斷T1和T2類型是否一致

最后,可以使用gmock中的各種matchers來進行斷言:

Fatal assertion Nonfatal assertion Verifies
ASSERT_THAT(value, matcher); EXPECT_THAT(value, matcher); value matches matcher

matchers 見gmock 的介紹

參考鏈接:https://github.com/google/googletest/blob/master/googletest/docs/advanced.md


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM