Assertions vs expectations

With Google Test we can test a condition with an assertion of with an expectation.

A failed assertion it is translated in a fatal failure, so the current function is aborted. If we don't want this to happen - maybe because we have some cleanup code that has to be performed even in case of failure - we can use an expectation, that leads to a non-fatal failure.

A couple of test functions would show the difference:

TEST(AssertVsExpect, Assert) {
ASSERT_TRUE(false) << "*** false is not true!";
ASSERT_TRUE(false) << "Execution won't get here";
}

TEST(AssertVsExpect, Expect) {
EXPECT_TRUE(false) << "*** false is not be expected to be true";
EXPECT_TRUE(false) << "*** repeated test on false";
}

In the first test, only the first check is performed. Notice the use of the "put to" (<<) operator to add some details to the test result.

No comments:

Post a Comment