Question

I want to test a assertion with gtest. The method looks like this:

void aMethod()
{
    Q_ASSERT( 1 == geode.getNumDrawables());

    DoSomeOtherStuff
}

And the test looks like this:

TEST_F(aTestClassDeathTest, aTestName)
{
    ::testing::FLAGS_gtest_death test_style = "threadsafe";
    ASSERT_DEATH({ aMethod;}, "1 == geode.getNumDrawables");
}

This test works fine with gcc under linux.

But there is a problem with this test in visual studio 2010 under windows.

When I run the tests, an error window open. It shows an assertion failure. When I close the window, all tests finish. There are no test failures.

I think gtests starts a new process that cause the assertion failure and evaluates the process output. This works and the test pass.

But when visual studio notice that the new process fails, it creates the error windows.

How can I suppress the error window?

Was it helpful?

Solution

The popup window is due to Qt, not gtest. The Q_ASSERT macro is invoking the CRT debug window.

You can either replace your Q_ASSERT with assert or you can use a QtMsgHandler to suppress the popup.

OTHER TIPS

As pointed out by other user (Fraser) in this thread, Googletest does not catch assertions thrown from Qt for ex. Q_ASSERT or Q_ASSERT_X. That is, user needs to take action on UI dialog shown from the Qt application.

QtMsgHandler comes to rescue. Here is the way you can go around this issue.

define a function as shown below:

void myMessageOutput(QtMsgType type, const char *msg)
 {
     switch (type) 
     {
         case QtDebugMsg:
             fprintf(stdout, "Debug: %s\n", msg); // you can also use stderr if you want
             break;
         case QtWarningMsg:
             fprintf(stdout, "Warning: %s\n", msg);
             break;
         case QtCriticalMsg:
             fprintf(stdout, "Critical: %s\n", msg);
             break;
         case QtFatalMsg:
             fprintf(stdout, "Fatal: %s\n", msg);
             abort();
     }
 }

In your Googletest application where you are expecting assertion call it in following manner:

// Redirect all messages generated from Qt to stdout
qInstallMsgHandler(myMessageOutput);
// Call death testcase
EXPECT_DEATH(call_causing_assertion(),"");
// Restore the default message handler
qInstallMsgHandler(0);

You can also make call in following manner to suppress all Qt assertion dialogs from test application:

int main(int argc, char **argv)
{
 qInstallMsgHandler(myMessageOutput);
 //QApplication app(argc, argv);

 testing::InitGoogleTest(&argc, argv);
 return RUN_ALL_TESTS();

 //...
 //return app.exec();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top