Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ASSERT and EXPECT in GoogleTest

While ASSERT_* macros cause termination of test case, EXPECT_* macros continue its evaluation. I would like to know which is the criteria to decide whether to use one or the other.

like image 427
martjno Avatar asked Apr 02 '10 06:04

martjno


People also ask

When to Use assert vs expect?

ASSERT: Fails fast, aborting the current function. EXPECT: Continues after the failure.

How do I compare strings in Gtest?

String Comparison If you want to compare two string objects, use EXPECT_EQ , EXPECT_NE , and etc instead. Note that "CASE" in an assertion name means that case is ignored. A NULL pointer and an empty string are considered different.

What is Expect_no_throw?

EXPECT_NO_THROW. EXPECT_NO_THROW( statement ) ASSERT_NO_THROW( statement ) Verifies that statement does not throw any exception.


1 Answers

Use ASSERT when the condition must hold - if it doesn't the test stops right there. Use this when the remainder of the test doesn't have semantic meaning without this condition holding.

Use EXPECT when the condition should hold, but in cases where it doesn't we can still get value out of continuing the test. (The test will still ultimately fail at the end, though.)

The rule of thumb is: use EXPECT by default, unless you require something to hold for the remainder of the tests, in which case you should use ASSERT for that particular condition.


This is echoed within the primer:

Usually EXPECT_* are preferred, as they allow more than one failures to be reported in a test. However, you should use ASSERT_* if it doesn't make sense to continue when the assertion in question fails.

like image 156
GManNickG Avatar answered Oct 09 '22 11:10

GManNickG