views:

121

answers:

3

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.

+1  A: 

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 use ASSERT_* if it doesn't make sense to continue when the assertion in question fails.

GMan
Standard example: before checking contents of container you ASSERT it's size, because it doesn't make sense to check EXPECTations on items that might not even exist (besides, your test would most likely crash if you try to get nth element of empty container).
chalup
+2  A: 

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.

nabulke
A: 

Check the following link: Effective C++ Testing Using GoogleTest (slide 23). There is a good guideline/advice on the use of EXPECT vs ASSERT.

ratkok