Forum Discussion

North-E's avatar
North-E
Occasional Contributor
16 years ago

Handling exceptions in different units

Hi, everyone!



I'm new in testing, so need your advice about running script tests for my Visual Studio C++ application in TestComplete 7 (JScript language is used).

I have main script unit called "Testing" where test routines are placed. All manipulations with application's interface are discribed by functions in other units which included in "Testing" by //USEUNIT...

I created few test items and then ran the project. During project execution the application may crash, and then script stops with an exception or/and error. These exceptions occur in other script units. I tried to catch them using "try...catch" construnction in "Testing" but it doesn't work. Therefore, TestComplete project stops with following message: "The following error occured while the element specified in the "Test-case #0002" test item was being executed. Do you want to stop the project execution?". I had to say "No" manually everytime I wanted to continue testing by next test item. But how can I catch all exceptions in one place to avoid TestComplete questions and continue project run automatically?

I tried to set "Stop on exception" and "Stop on error" for test items but these options are not suitable in this case.

Sometimes I also receive "Visual Studio++ Runtime Error" window when the application crashes. And seemed to me, this is not unexpected window or I still don't know how to handle it (project's playback options for unexpected windows are "Click on focused control" and below).



Thanks a lot for attention. Hope for your answers. Any advice will be useful...

22 Replies

  • gaccardo's avatar
    gaccardo
    New Contributor

    There was a post on SQA Forums several years ago about how to handle this issue.

    Essentially, it is a 4 step process.  Overall, it is awkward and a bit ugly, but it does

    work.


    1. Create an exception class that can be used to percolate exceptions up the call stack.


    function CustomException (value1, value2)

    // Actually, this class can take mutiple values, not just strings.

    // Unfortunately, I can't share our implementation.

    {

      this.message = value1

      this.description = value2

    }


    2. Use try/catch blocks to translate exceptions into return values of your exception class.

    function Test1 ()

    {

         var returnValue = null;

         try

        {

             // test case goes here.

         }

         catch (exception)

         {

              returnValue = new CustomException (exception);

         }


         return (returnValue);

    }


     


    3. Add a CheckResult () wrapper function in every script unit and use it when calling other methods.


    function BiggerTest ()

    {

         CheckResult (Test1 ())

    }


    function CheckResult (value)

    {

         if (value instanceof CustomException)

         {

              throw value;

         }

         else

         {

              return (value);

         }

    }


    4. Deal with the exceptions at the appropriate level, usually at the level of the Main () function.


    Happy Testing,

    Glen Accardo