[googletest] treating c++ exceptions as test failures

1,767 views
Skip to first unread message

Keith Ray

unread,
Apr 30, 2010, 2:32:07 PM4/30/10
to Google C++ Testing Framework
I like to catch c++ exceptions and treat them as test failures, continuing to run other tests, but NOT catch runtime errors (like illegal memory access) -- I want the debugger to catch those.

So I currently have some code like the following (for all platforms, not just windows).  I can rewrite this to only do try/catch based off of GTEST_FLAG(catch_exceptions) and submit a patch, but that flag seems to be used for windows-runtime-errors rather than what I want. 

Would we want a new flag?   Is anyone interested in a patch like this?

// Runs the test and updates the test result.

void Test::Run() {

  if (!HasSameFixtureClass()) return;


  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();

  impl->os_stack_trace_getter()->UponLeavingGTest();

  try {

    SetUp();

  } catch(const std::exception& ex) {

AddExceptionThrownFailure(ex.what(), "SetUp()");

  } catch(...) { // this might need windows-specific code to NOT catch run-time errors.

AddExceptionThrownFailure("unknown exception", "SetUp()");

  }


  // We will run the test only if SetUp() had no fatal failure.

  if (!HasFatalFailure()) {

    impl->os_stack_trace_getter()->UponLeavingGTest();

    try {

      TestBody();

    } catch(const std::exception& ex) {

  AddExceptionThrownFailure(ex.what(), "the test body");

    } catch(...) {

  AddExceptionThrownFailure("unknown exception", "the test body");

    }

  }


  // However, we want to clean up as much as possible.  Hence we will

  // always call TearDown(), even if SetUp() or the test body has

  // failed.

  impl->os_stack_trace_getter()->UponLeavingGTest();

  try {

    TearDown();

  } catch(const std::exception& ex) {

AddExceptionThrownFailure(ex.what(), "TearDown()");

  } catch(...) {

AddExceptionThrownFailure("unknown exception", "TearDown()");

  }

}

// AddExceptionThrownFailure altered as well.


--
C. Keith Ray, IXP Coach, Industrial Logic, Inc.
http://industriallogic.com      866-540-8336 (toll free)
Groove with our Agile Greatest Hits: http://www.industriallogic.com/elearning/
http://agilesolutionspace.blogspot.com/

Vlad Losev

unread,
May 1, 2010, 7:02:57 PM5/1/10
to Keith Ray, Google C++ Testing Framework
Hi Keith,

We are tracking this in the issue 44, the one reported back ago. We haven't forgotten that, and plan to have this in release 1.6, before the end of Q2.

On Fri, Apr 30, 2010 at 10:32 PM, Keith Ray <keit...@gmail.com> wrote:
I like to catch c++ exceptions and treat them as test failures, continuing to run other tests, but NOT catch runtime errors (like illegal memory access) -- I want the debugger to catch those.

So I currently have some code like the following (for all platforms, not just windows).  I can rewrite this to only do try/catch based off of GTEST_FLAG(catch_exceptions) and submit a patch, but that flag seems to be used for windows-runtime-errors rather than what I want. 

Would we want a new flag?   Is anyone interested in a patch like this?

Generally, an exception escaping the test code is the sign of a problem. If an exception is expected in the code under test, the test should intercept it with a Google Test's exception testing construct. As such, I think we should intercept and log as failures all C++ exceptions if they are enabled in the compiler.

GTEST_FLAG(catch_exceptions) can still catch Windows SEH exceptions when supplied. As I pointed in the issue, it should play well with the C++ exception catching mechanism.

Any other ideas/suggestions?

Thanks,
Vlad
Reply all
Reply to author
Forward
0 new messages