| yanfali ( @ 2008-05-03 19:42:00 |
Testing Anti Pattern: Testing Exceptions
Here's the wrong way to test exceptions in C++ and Java:
Here's the idiomatic way:
Why bother with the CPPUNIT_ASSERT(true)? Because it conveys intent. The Exception is the correct path. This idiom comes from JUnit, learn it, use it.
Here's the wrong way to test exceptions in C++ and Java:
bool gotExcep = false;
try {
callSomethingThatThrows();
} catch (exception &e) {
gotExcep = true;
}
CPPUNIT_ASSERT(gotExcept);
Here's the idiomatic way:
try {
callSomethingThatThrows();
CPPUNIT_FAIL("call something should have thrown an exception!");
} catch (exception &e) {
CPPUNIT_ASSERT(true);
}
Why bother with the CPPUNIT_ASSERT(true)? Because it conveys intent. The Exception is the correct path. This idiom comes from JUnit, learn it, use it.