Search code examples
c++unit-testingthrowcppunit

How to assert in cppunit that a statement throws an exception either of type Excp1 or Excp2?


CPPUNIT_ASSERT_THROW(Expression, ExceptionType) does not seem to allow checking for exceptions of multiple types i.e. for a statement that can throw more than one kind of exceptions.

For e.x. an expression may throw Excp1 on one platform, or Excp2 on another platform. Is there a workaround to test such statements using CPPUNIT_ASSERT_THROW?


Solution

  • First test, you make your test conditions such that it throws exeption 1. If it fails to throw, that is a test failure. if it does throw, you catch it as an exception 1, and accept it as passing. If it throws something else, the framework catches it.

    Second test, you make using conditional compilation to enable code for platform 2 only. You make your test conditions such that it throws exception 2. IF it fails to throw, that is a test failure. If it does throw, you catch it as exception 2, and accept it as passing. If it throws something else, the framework catches it.

    On the first platform the test simply passes, as there is nothing for it to do. On the second platform you catch exception 2 as expected.