'Gtest EXPECT_DEATH() fails with SIGABRT

I'm testing an assertion with EXPECT_DEATH() and it fails on Ubuntu (gitlab ci pipeline) with killed by signal 6 SIGABRT. However, exact same code works fine on Windows.

Afaik, this macro is specifically designed to handle exit with error codes.

#include "gtest/gtest.h"

class MyDeathTest : public ::testing::Test {
 protected:
 ...
};

TEST_F(MyDeathTest, TestNegative1) {
  EXPECT_DEATH(assert(false), "");
}

I've tried to use EXPECT_EXIT(assert(false), ::testing::KilledBySignal(SIGBART)), ""); instead and ::testing::ExitedWithCode(6), as well as EXPECT_DEBUG_DEATH(), EXPECT_DEATH_IF_SUPPORTED() etc. with no luck.

Also, i've tried to set GTEST_FLAG_SET(death_test_style, "threadsafe"); but got error: 'GTEST_FLAG_SET' was not declared in this scope;

Any suggestions on fix?



Solution 1:[1]

A couple of things:

  1. You should use SIGABRT, not SIGBART
  2. Looks like the google test release that you are using is old and doesn't have the GTEST_FLAG_SET macro yet. You can use this as an alternative:
(void)(::testing::GTEST_FLAG(death_test_style) = "threadsafe");

Working example: https://godbolt.org/z/15azKeMG4

From googletest docs

In googletest, death tests are run in a child process and the way they work is delicate. To write death tests you really need to understand how they work—see the details at Death Assertions in the Assertions Reference.

In particular, death tests don’t like having multiple threads in the parent process. So the first thing you can try is to eliminate creating threads outside of EXPECT_DEATH(). For example, you may want to use mocks or fake objects instead of real ones in your tests.

Sometimes this is impossible as some library you must use may be creating threads before main() is even reached. In this case, you can try to minimize the chance of conflicts by either moving as many activities as possible inside EXPECT_DEATH() (in the extreme case, you want to move everything inside), or leaving as few things as possible in it. Also, you can try to set the death test style to "threadsafe", which is safer but slower, and see if it helps.

So I would say first make sure you are using the latest release, and secondly try with threadsafe.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ari