How to handle exceptions in text fixtures

2,010 views
Skip to first unread message

Michael Repucci

unread,
Dec 27, 2010, 10:44:18 AM12/27/10
to Google C++ Testing Framework
I'm pretty new to the Google Testing Framework. I'm using version
1.5.0 with Visual Studio 2010.

From the Primer documentation, in the section "Invoking the Tests", we
learn: "In addition, if the text fixture's constructor generates a
fatal failure in step 2, there is no point for step 3 - 5 and they are
thus skipped." This implies to me that the tests which depend upon
that test fixture should return failure. However, what I experience is
that the entire process aborts, and no further tests are run.

For example:

class MyTest : public testing::Test {
public:
MyTest() { throw std::runtime_error("test"); }
};

TEST_F(MyTest,Test1) {
}

TEST_F(TestFixtureThatDoesntThrow,TestA) {
SUCCESS();
}

will prevent TestA from running, if Test1 gets run first. Am I doing
something wrong? Is there a way to have exceptions in a text fixture's
constructor (and destructor) be reported as a failure, rather than
terminate the process? Sorry if I missed this explanation in the
documentation, but I didn't see it covered. Thank you for the help!

Zhanyong Wan (λx.x x)

unread,
Dec 28, 2010, 12:56:42 PM12/28/10
to Michael Repucci, Google C++ Testing Framework
This (exception handling) was implemented after 1.5.0 was released.
You can get it by using the head revision of the SVN trunk. Or wait
for 1.6.0 if you are patient.

--
Zhanyong

Steve Jaffe

unread,
Dec 28, 2010, 3:17:34 PM12/28/10
to Michael Repucci, Google C++ Testing Framework
What I do is make the body of the test function a try block, with catch
reporting the error, eg

TEST_F(MyTest, Test1)
try
{
//whatever
}
catch(...)
{
FAIL() << "exception" << std::endl;
}

where you could of course have multiple catch blocks depending on the type of
exception with more informative reporting.

If you want to reduce the boilerplate, define macros so that this becomes

TEST_F(MyTest, Test1)
BEGIN_TEST
{
//whatever
}
END_TEST

Joey Oravec

unread,
Dec 28, 2010, 3:57:26 PM12/28/10
to Steve Jaffe, Michael Repucci, Google C++ Testing Framework
Steve,
 
You have a useful boilerplate there, but it catches exceptions inside the fixture's TestBody() and Michael described an exception that gets thrown from the fixture's constructor. I don't expect it would catch what he described.
 
I checked SVN head and Test::Run() uses a function to call SetUp(), TestBody(), and TearDown() within a try-catch block if exception catching is enabled. Somebody smarter than me would have to answer what happens when an exception is thrown from the fixture's constructor. I don't know what would happen or where the exception would get caught.
 
-joey

Zhanyong Wan (λx.x x)

unread,
Dec 28, 2010, 4:39:47 PM12/28/10
to Joey Oravec, Steve Jaffe, Michael Repucci, Google C++ Testing Framework
Joey,

On Tue, Dec 28, 2010 at 12:57 PM, Joey Oravec <joeyo...@gmail.com> wrote:
> Steve,
>
> You have a useful boilerplate there, but it catches exceptions inside
> the fixture's TestBody() and Michael described an exception that gets thrown
> from the fixture's constructor. I don't expect it would catch what he
> described.
>
> I checked SVN head and Test::Run() uses a function to call SetUp(),
> TestBody(), and TearDown() within a try-catch block if exception catching is
> enabled. Somebody smarter than me would have to answer what happens when an
> exception is thrown from the fixture's constructor. I don't know what would
> happen or where the exception would get caught.

You are right that gtest SVN head doesn't handle exceptions thrown in
a test fixture ctor yet. I've re-opened issue 44 to track this and
some related cases. Thanks,

--
Zhanyong

Steve Jaffe

unread,
Dec 28, 2010, 5:20:42 PM12/28/10
to Joey Oravec, Google C++ Testing Framework
True, but the same trick would work for the constructor (and SetUp() if you use that). 

Michael Repucci

unread,
Dec 29, 2010, 10:15:56 AM12/29/10
to Zhanyong Wan (λx.x x), Joey Oravec, Steve Jaffe, Google C++ Testing Framework
I can confirm that Joey is correct that Steve's boilerplate for catching exceptions does not catch exceptions in the test fixture's constructor.

I have also tried the latest revision (r528) of gtest, and even without explicitly specifying the command-line flag --gtest_catch_exceptions, the exception in the constructor is caught.

Thank you to everyone for your replies!

:) Michael

2010/12/28 Zhanyong Wan (λx.x x) <w...@google.com>

Steve Jaffe

unread,
Dec 29, 2010, 1:25:51 PM12/29/10
to Michael Repucci, Google C++ Testing Framework
If you add it to the constructor then the constructor cannot emit any exceptions; this is independent of anything the gtest framework does or doesn't do.  Of course it is good if the framework catches exceptions, however it can't report any information specific to the types that you are throwing.


From: Michael Repucci <mic...@repucci.org>
To: Zhanyong Wan (λx.x x) <w...@google.com>
Cc: Joey Oravec <joeyo...@gmail.com>; Steve Jaffe <steve...@yahoo.com>; Google C++ Testing Framework <googletes...@googlegroups.com>
Sent: Wed, December 29, 2010 10:15:56 AM
Subject: Re: [googletest] How to handle exceptions in text fixtures

Zhanyong Wan (λx.x x)

unread,
Dec 29, 2010, 1:40:45 PM12/29/10
to Joey Oravec, Steve Jaffe, Michael Repucci, Google C++ Testing Framework
2010/12/28 Zhanyong Wan (λx.x x) <w...@google.com>:

> Joey,
>
> On Tue, Dec 28, 2010 at 12:57 PM, Joey Oravec <joeyo...@gmail.com> wrote:
>> Steve,
>>
>> You have a useful boilerplate there, but it catches exceptions inside
>> the fixture's TestBody() and Michael described an exception that gets thrown
>> from the fixture's constructor. I don't expect it would catch what he
>> described.
>>
>> I checked SVN head and Test::Run() uses a function to call SetUp(),
>> TestBody(), and TearDown() within a try-catch block if exception catching is
>> enabled. Somebody smarter than me would have to answer what happens when an
>> exception is thrown from the fixture's constructor. I don't know what would
>> happen or where the exception would get caught.
>
> You are right that gtest SVN head doesn't handle exceptions thrown in
> a test fixture ctor yet.  I've re-opened issue 44 to track this and
> some related cases.  Thanks,

Vlad just pointed out to me that the SVN head actually already handles
exceptions thrown in test fixture ctor/dtor and several other places
as well. I verified it by looking at the code and the unit tests. So
I closed issue 44. Sorry for the noise.

--
Zhanyong

Reply all
Reply to author
Forward
0 new messages