Search code examples
c++unit-testinggoogletestgooglemock

Wrong function call's evaluation when in death test


I'm writing tests using gtest and gmock. Most of my test cases are supposed to crash with an custom assert (that I mock). Here come troubles : whereas the assert is well triggered, I have plenty of problems with the expected calls.

The following code is the three steps I have been through in order to make it works ('cause yes, this part works) :

class MyTestedObject : public testing::Test {
public:
    static MockObject * myMockedObject;
};

void assertFailure() {
    exit(1);
}

TEST_F(MyTestedObjectDeathTest, nullInputConstructors) {
    MockAssertHandler assertHandler;
    EXPECT_CALL(assertHandler, failure(_,_,_,_))
    .Times(1)
    .WillRepeatedly(InvokeWithoutArgs(assertFailure));
    setHandler(assertHandler);

    testing::Mock::AllowLeak(myMockedObject);
    testing::Mock::AllowLeak(&assertHandler);

    EXPECT_DEATH(new MyTestedObject(NULL, NULL,0), ".*");
}

MyTestedObject's constructor begins to check whether arguments are NULL. It's supposed to trigger an assert if at least one of them is. But the test fails because failure is 'never called'. Debug reveals it is called.

Then I tried to comment the Times part, just to be sure it came from here and it was the only issue. It works, but is not adequate : I want to be sure the program die from my assert. As the evaluation of EXPECT_CALL is done when the mock object is destroyed, I guessed the exit calls was messing up the whole thing, so I tried this, which works :

void testHelper() {
    MockAssertHandler assertHandler;
    EXPECT_CALL(assertHandler, failure(_,_,_,_))
    .Times(1)
    .WillRepeatedly(InvokeWithoutArgs(assertFailure));
    setHandler(assertHandler);

    testing::Mock::AllowLeak(MyTestedObjectTest::myMockObject);
    testing::Mock::AllowLeak(&assertHandler);

    new MyTestedObject(NULL, NULL,0);
}

TEST_F(MyTestedObjectDeathTest, nullInputConstructors) {
    EXPECT_DEATH(testHelper(), ".*");
}

Now, I'd like to be sure no calls are made on others functions.

I tried to put myMockedObject in a StrictMock structure and to put EXPECT_CALL(...).Times(0), but I got the same pattern as at first : the 'exit' call seems to block all EXPECT_CALL evaluation.

Any hint /workaround ? :)

EDIT : Forgot to tell : Execution env. is Windows 7 with Visual Studio 2008.


Solution

  • Google Test's wiki explains this:

    Since statement runs in the child process, any in-memory side effect (e.g. modifying a variable, releasing memory, etc) it causes will not be observable in the parent process.

    That includes Google Mock's tracking of calls made in the death test statement. In short, Google Mock and death tests don't mix.

    My advice here is to separate those tests. Use Google Mock to verify that a failure handler is invoked and use death tests to verify that your failure handler does indeed do what it's supposed to (terminate the program, print specified output, etc.)