mardi 30 août 2016

write death test to verify std::set_terminate behavior

I'm using gtest to write some unit tests for exception handling code. As a simple example, let's say I want my program to abort on any unhandled exception, so I create the following test:

TEST_F(myFixture, myTest)
{
    std::set_terminate([](){std::abort(); });

    EXPECT_DEATH(throw std::runtime_error("terminate"), ".*");
}

I would expect this to succeed, since abort should be called on the program. However, my actual result is:

error: Death test: throw std::runtime_error("terminate")

Result: threw an exception.
Error msg:

[ DEATH ]
[ DEATH ] main.cpp(122):: Caught std::exception-derived exception escaping the death test statement. Exception message: terminate

[ DEATH ]

I think gtest's exception handlers are getting in the way. I've tried disabling them using the GTEST_CATCH_EXCEPTIONS environment variable, and the --gtest_catch_exceptions=0 command line flag as mentioned in the advanced guide, but I get the same result.

Am I doing something wrong, or is "death by exception" not something gtest can test for?

Aucun commentaire:

Enregistrer un commentaire