Use ASSERT_TRUE() in a function which has return value

6,412 views
Skip to first unread message

jun...@chromium.org

unread,
Aug 21, 2017, 12:43:35 PM8/21/17
to Chromium-dev
In some test code, I tried using ASSERT_TRUE() in a function which has bool return type. The compiler gives the following error:
error: cannot initialize return object of type 'bool' with an rvalue of type 'void'

Does it mean ASSERT_TRUE() can only be used in functions that return void? Thanks!

Ken Rockot

unread,
Aug 21, 2017, 12:49:27 PM8/21/17
to jun...@chromium.org, Chromium-dev
Yes, ASSERT* macros generate code which can return with no value.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/62cda982-bc2e-4665-84da-abbacd04b592%40chromium.org.

Charles Harrison

unread,
Aug 21, 2017, 12:50:40 PM8/21/17
to jun...@chromium.org, Chromium-dev
Yes, ASSERT_* methods return from the method but do not return any value.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.

jun...@chromium.org

unread,
Aug 21, 2017, 7:10:11 PM8/21/17
to Chromium-dev
Thanks for the answer!

Adam Rice

unread,
Aug 22, 2017, 2:44:56 AM8/22/17
to jun...@chromium.org, Chromium-dev
As a general rule, EXPECT_* and ASSERT_* macros should only be used in the body of the test itself. For EXPECT_* this is a stylistic thing: it should be possible to tell at a glance what a test is testing. However, the important difference between ASSERT_* and EXPECT_* is that the ASSERT family of macros abort the test. If they are called inside a function that is not the body of the test, they cannot abort the test, so it's usually wrong.

Unfortunately, many tests in Chromium code violate this rule.

On 22 August 2017 at 08:10, <jun...@chromium.org> wrote:
Thanks for the answer!

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.

Reilly Grant

unread,
Aug 22, 2017, 11:29:43 AM8/22/17
to ri...@chromium.org, jun...@chromium.org, Chromium-dev
I would suggest that EXPECT_* be allowed outside the body of the test in helper functions, mocks, etc. as it is helpful for debugging a test failure to see exactly where an unexpected condition occurred rather than saving some state about the condition and EXPECTing it later in the test body.

Joe Mason

unread,
Aug 22, 2017, 12:59:16 PM8/22/17
to Reilly Grant, Adam Rice, jun...@chromium.org, Chromium-dev
I tend to use the style that helper functions which do some work and then call EXPECT_* on the results are named "ExpectFoo", so that they're easy to spot in the calling tests.

There are ways to get ASSERT_* in a helper function to abort the test correctly, but you should try not to use them because they add extra complexity.

Reply all
Reply to author
Forward
0 new messages