Menu

Specific exception handling

2002-03-22
2002-03-28
  • Edouard Mercier

    Edouard Mercier - 2002-03-22

    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?

    Edouard

     
    • Baptiste Lepilleur

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

      Baptiste.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.