I have found an unexpected behavior of Google Test, when it comes to death tests combined with expectations on mock objects.
Check the following example:
#include <gmock/gmock.h>
#include <cassert>
class Interface
{
public:
virtual void foo() = 0;
};
class InterfaceMock : public Interface
{
public:
MOCK_METHOD0(foo, void());
};
class Bar
{
public:
void call(Interface& interface)
{
interface.foo();
assert(false);
}
};
TEST(BarTest, call_fooGetsCalledAndDies)
{
InterfaceMock mock;
EXPECT_CALL(mock, foo());
ASSERT_DEATH({ Bar().call(mock); }, "");
}
Test fails because expected call to mock.foo()
fails to assert:
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from BarTest
[ RUN ] BarTest.call_fooGetsCalledAndDies
[WARNING] /tmp/tmp.sHHkM/gmock-1.7.0/gtest/src/gtest-death-test.cc:825:: Death tests use fork(), which is unsafe particularly in a threaded context. For this test, Google Test couldn't detect the number of threads.
foo.cc:31: Failure
Actual function call count doesn't match EXPECT_CALL(mock, foo())...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] BarTest.call_fooGetsCalledAndDies (1 ms)
[----------] 1 test from BarTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (2 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] BarTest.call_fooGetsCalledAndDies
1 FAILED TEST
Interestingly, if the EXPECT_CALL(mock, foo())
line is commented, Google Mock raises a warning for an unexpected call to mock.foo()
:
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from BarTest
[ RUN ] BarTest.call_fooGetsCalledAndDies
[WARNING] /tmp/tmp.sHHkM/gmock-1.7.0/gtest/src/gtest-death-test.cc:825:: Death tests use fork(), which is unsafe particularly in a threaded context. For this test, Google Test couldn't detect the number of threads.
GMOCK WARNING:
Uninteresting mock function call - returning directly.
Function call: foo()
Stack trace:
[ OK ] BarTest.call_fooGetsCalledAndDies (1 ms)
[----------] 1 test from BarTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
I guess this is somehow related to the warning on death tests using fork()
and threads, but I fail to match all pieces together.
This is a known limitation of death tests. Internally assert_death
forks, so mocks called in the child are not registered in the parent process. If you wish to suppress the warnings, consider using NiceMock
.