Forum Discussion

jose_pita's avatar
jose_pita
Super Contributor
12 years ago

How to know from which test an error came from at runtime

Hi Guys, 



I have this function handling the error event:




function GeneralEvents_OnLogError(Sender, LogParams)


{


    


  if(LogParams.Priority >300)


    Runner.Stop(true);


}



And I want to know from which test the error came from, so that I could skip that "if" even if the priority is biggen than 300.



I've checked the "Sender" and "LogParams" and nothing useful appears.



Thanks for the help

7 Replies

  • Depending on whether you're catching exceptions anywhere in your call stack, 



    arguments.caller.callee.toString()



    may help.
  • You can create your own library to log messages/errors. For example,



    Create a script called 'Logger' and a function called ErrorMessage(strMsg, sourceOfError,Priority)

    {



       if(!priority)

        priority = 100; //default value



      if(priority>300)

      {

        

      throw new error(666,sourceOfError + " says:" + strMsg);





      }





    }





    And from within your test 



    function testMyTestase()

    {



     try

     {

      //do something



       if (somethingDidnotwork)

        {

        Logger.Error("Oops, better luck next time", testMyTestase,3000)

        }

     catch(e)

     {

        Log.Error(e.description); //this will print  'testMyTestase says Oops, better luck next time'

         



      }

    finally

     {

       //optional - incase you want to clean up



     }











    }
  • If you're running the tests using TestComplete's test items for a project you could read in the current test using the following:



    myTest = Split(Project.TestItems.Current.ElementToBeRun.Caption, " - ")(1)
  • jose_pita's avatar
    jose_pita
    Super Contributor
    Eero, that kinda works, as long as I'm running the project, but if I'm running a test outside the project it doesn't work.



    What I mean is, when I'm building the test, I'm running it all the time, and in that case it doesn't recognise the Project.TestItem...