Using ASSERT and EXPECT in GoogleTest
Asked Answered
I

4

76

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.

Irrawaddy answered 2/4, 2010 at 6:21 Comment(1)
ASSERT_* macros do not (necessarily) terminate the test case, please see Martin's answer for details.Cultism
S
93

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.

Stickweed answered 2/4, 2010 at 6:24 Comment(3)
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).Backcourt
It's not about whether or not you want your program to run, it's about whether or not you want the test suite to continue running. An EXPECT failure should mean that the code you are testing is defective. An ASSERT failure should mean that the test suite itself is defective or, that the code you are testing is so messed up that there's no point in continuing to try to test it.Decastro
@SolomonSlow 's distinction is key.Mccusker
C
30

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.

Cyclotron answered 2/4, 2010 at 6:30 Comment(0)
D
10

In addition to previous answers...

ASSERT_ does not terminate execution of the test case. It returns from whatever function is was used in. Besides failing the test case, it evaluates to return;, and this means that it cannot be used in a function returning something other than void. Unless you're fine with the compiler warning, that is.

EXPECT_ fails the test case but does not return;, so it can be used inside functions of any return type.

Dorinda answered 10/2, 2017 at 16:5 Comment(0)
K
0

ASSERT_*:

  • when it fails, won't check the remaining conditions.

EXPECT_*:

  • when it is failed, it still checks the remaining conditions.
Kinsman answered 23/11, 2023 at 5:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.