Extending handling of unexpecting exceptions

  • Anders Malmborg

    Anders Malmborg - 2003-05-06

    Edouard Mercier posted a request quite a while ago. I have attached it below. As it was a long time ago, I'm about to open a new thread...

    I have the following "problem". Within a test case an exception of other kind as foreseen in
    TestCase::run( TestResult *result ) can occure.
    It is a failure of the test case and should not be tested with CPPUNIT_TEST_EXCEPTION macro.
    The analyzing of the error would be supported by the output of the exception message.

    One possible solution could be to add a new catch block to the code in TestCase.cpp.
    Is this a "recommended" solution?

    My first idea was to sub class the TestCase class and overwrite the
    run method. Then I will have to make additional programming to use the sub classed TestCase
    instead of the base TestCase.

    Then I detected the TestDecorator. After studing the documentation and searching the web for an
    evening, I still do not understand how to use a Decorator to handle unexpected exceptions....

    Recommedations, Suggestions anyone?


    By: edouard_mercier ( Edouard Mercier )
      Specific exception handling
    2002-03-22 01:14

    hello everybody;

    first of all, thank you very much for having ported the must JUnit to C++ !

    I would like to post a remark concerning CppUnit framework as far as test fixture exceptions are concerned.

    My discussion concerns the use of <code>TesCaller</code> template use so as to run various tests in the same fixture: let's say I have a <code>TestA</code> class, which is derived from <code>CppUnit::TestCase</code>; that I have coded a <code>test1</code> and <code>test2</code> method; that I have created a suite with the <code>TestCaller</code> template class so that those 2 tests are ran consecutively.

    Inside my test fixtures (<code>test1</code> and <code>test2</code>), some method calls may throw home-made exceptions: if not properly caught in the test fixture method, the exception is bound to be propagated untill the <code>TestCaller</code> template class and will be considered as a (...) exception. Thus, you have to handle home-made exceptions in your fixture tests !

    Now, you're going to tell me, make your own <code>TestCaller</code> derived class so as to handle the exception as you want in its <code>void run(CppUnit::TestResult * result)</code> or in its <code>void runTest()</code> methods. This is what I did; however, if you want this exception to trigger your test listeners (<code>TestListener</code> class) <code>virtual void addFailure (Test *test, Exception *e)</code>, you have also to create your own <code>TestResult</code> class !

    That's the point I wanted to emphasize: you have to derive two classes if you use the <code>TestCaller</code> template class when you do not want to handle at all specific exceptions in the fixtures, and that those exceptions should be propagated to the test listeners: isn't it a bit cumbersome?

    Do you have a better idea?


    By: blep ( Baptiste Lepilleur )
      RE: Specific exception handling 
    2002-03-28 12:23

    If you know that an exception will be thrown, why not use CPPUNIT_TEST_EXCEPTION ?


    • Baptiste Lepilleur

      Check the Protector functionnalities starting with CppUnit 1.9.10.

      The NEWS file describe the functionnalities. Also check up the ML, someone already asked the question there.


    • Anders Malmborg

      Anders Malmborg - 2003-05-15

      Protector is what I need.



Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks