googletest icon indicating copy to clipboard operation
googletest copied to clipboard

GTEST_FAIL_AT() doesn't return

Open ludocode opened this issue 2 years ago • 0 comments

GTEST_FAIL() is defined through these macros, ultimately ending up at GTEST_MESSAGE_AT_():

#define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed")
#define GTEST_FATAL_FAILURE_(message) \
  return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure)
#define GTEST_MESSAGE_(message, result_type) \
  GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type)

Notice the return statement which causes it to stop execution of the test.

However, GTEST_FAIL_AT() is defined like this:

#define GTEST_FAIL_AT(file, line)         \
  GTEST_MESSAGE_AT_(file, line, "Failed", \
                    ::testing::TestPartResult::kFatalFailure)

It's missing a return statement before GTEST_MESSAGE_AT_(). The result of this is that test execution continues even after the GTEST_FAIL_AT(). For example:

TEST(Foo, Bar) {
    GTEST_FAIL_AT("foo.c", 5) << "fail";
    abort();
}

Here the test runner aborts because GTEST_FAIL_AT() didn't return. If you use GTEST_FAIL() instead, it returns, correctly skipping the call to abort.

ludocode avatar Oct 15 '22 08:10 ludocode