Wednesday, April 27, 2005

CPPUNIT: constructor/destructor VS setUp/tearDown

In C++, we used to use constructor and destructor to manage resources.
In XUNIT, setUp() and tearDown() are favored instead. One of the reasons
is that some langurages like Java have only undetermined garbage collector,
while we need a "determined" way to clean up the state after each test.

But in C++, we can use stack. As mentioned in
Herb Sutter's OOPSLA keynote, this approach has its own
advantage. So when writing tests with CPPUNIT, I still prefer constructor/destructor over setUp/tearDown, and it works.

There is a disadvantage, althrough it should be a implementation issue
of CPPUNIT: TestFixtures' Constructor will be called before main() to register
themself. So if your constructor fails, the whole program won't run at all.
In this case, you have to call setUp().


Post a Comment

<< Home