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.
Use ASSERT
when it's critical the test passes. (For example, if it doesn't pass then the rest of the program won't work.) Use EXPECT
when it's a test that can afford to fail (and will allow you to run your program.)
The rule of thumb is: use EXPECT
unless you need something to work for the entirety for the tests, in which case you should use ASSERT
since going on is meaningless.
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 useASSERT_*
if it doesn't make sense to continue when the assertion in question fails.
Use EXPECT_ when you
- want to report more than one failure in your test
Use ASSERT_ when
- it doesn't make sense to continue when the assertion fails
Since ASSERT_ aborts your function immediately if it fails, possible cleanup code is skipped. Prefer EXPECT_ as your default.
Check the following link: Effective C++ Testing Using GoogleTest (slide 23). There is a good guideline/advice on the use of EXPECT vs ASSERT.