Asserting with Google Test
The Google Test framework provides a rich set of both fatal and non-fatal assertion macros, which resemble function calls, to verify the tested code. When these assertions fail, the framework displays the source file, line number, and relevant error message (including custom error messages) to help developers quickly identify the failed code. We have already seen some simple examples on how to use the ASSERT_TRUE
macro; in this recipe, we will look at other available macros.
How to do it...
Use the following macros to verify the tested code:
- Use
ASSERT_TRUE(condition)
orEXPECT_TRUE(condition)
to check whether the condition istrue
andASSERT_FALSE(condition)
orEXPECT_FALSE(condition)
to check whether the condition isfalse
, as shown in the following code:EXPECT_TRUE(2 + 2 == 2 * 2); EXPECT_FALSE(1 == 2); ASSERT_TRUE(2 + 2 == 2 * 2); ASSERT_FALSE(1 == 2);
- Use
ASSERT_XX(val1, val2)
orEXPECT_XX(val1, val2)
to...