OveS - 2007-08-27

I have just started to use CppUnit and I am not sure if, what I have discovered,
is intended behaviour or if it is a bug? What I have found is that, if my setUp()
throws an exception the actual test function will not be called (just like I expected)
but tearDown() will still be called (which I did not expect).

I prefer tearDown() not being called if setUp() throws an exception.

A excerpt from the code for TestCase::run() shows that
void TestCase::run(TestResult* result)
{
  result->startTest(this) ;
  if (result->protect(TestCaseMethodFunctor(this,&TestCase::setUp),this,"setUp failed"))
  {
    result->protect(TestCaseMethodFunctor(this,&TestCase::runTest),this) ;
  }
  result->protect(TestCaseMethodFunctor(this,&TestCase::tearDown),this,"tearDown failed") ;
  result->endTest(this) ;
}

As can be seen from the code, only runTest is protected from exceptions thrown from setUp.

But, there is also a commented-out block of code with nested try/catch blocks that clearly
protects both runTest and tearDown from being called setUp throws.

I attempted to resolve this by deriving from TestCase and redefine run, and then use that
as base class when writing tests. That didn't work. My redefined run never got called since
all my tests became wrapped in TestCallers and TestCaller is derived from TestCase and it
doesn't call my redefined run.

So, should tearDown be protected from exceptions in setUp, and in that case, how?

/Ove