Performing More than One Verification in a Test Case

If the verification fails in a test case with only one verification statement, usually an exception is raised and the test case is terminated. However, if you want to perform more than one verification in a test case, before the test case terminates, this approach would not work.

Classic Agent Example

For example, see the following sample test case:
testcase MultiVerify ()
  TextEditor.Search.Find.Pick ()
  Find.VerifyCaption ("Find")
  Find.VerifyFocus (Find.FindWhat)
  Find.VerifyEnabled (TRUE)
  Find.Cancel.Click ()

The test case contains three verification statements. However, if the first verification, VerifyCaption, fails, an exception is raised and the test case terminates. The second and the third verification are not executed.

To perform more than one verification in a test case, you can trap all verifications except the last one in a do...except statement, like the following sample for the Classic Agent shows:
testcase MultiVerify2 ()
   TextEditor.Search.Find.Pick ()
   do
      Find.VerifyCaption ("Find")
   except
      ExceptLog ()
   do
      Find.VerifyFocus (Find.FindWhat)
   except
      ExceptLog ()
   Find.VerifyEnabled (TRUE)
   Find.Cancel.Click ()

All the verifications in this example are executed each time that the test case is run. If one of the first two verifications fails, the 4Test function ExceptLog is called. The ExceptLog function logs the error information in the results file, then continues the execution of the script.

Open Agent Example

For example, you might want to print the text associated with the exception as well as the function calls that generated the exception. The following test case illustrates this:
testcase VerifyTest ()
   STRING sTestValue = "xxx"
   STRING sExpectedValue = "yyy"
   CompValues (sExpectedValue, sTestValue)

CompValues (STRING sExpectedValue, STRING sTestValue)
   do
      Verify (sExpectedValue, sTestValue)
   except 
      ErrorHandler ()

ErrorHandler ()
   CALL Call
   LIST OF CALL lCall
   lCall = ExceptCalls ()
   Print (ExceptData ())
   for each Call in lCall
      Print("Module: {Call.sModule}", 
      "Function: {Call.sFunction}", 
      "Line: {Call.iLine}")
  • The test case calls the user-defined function CompValues, passing two arguments.
  • CompValues uses Verify to compare its arguments. If they are not equal, an exception is automatically raised.
  • If an exception is raised, CompValues calls a user-defined function, ErrorHandler, which handles the error. This is a general function that can be used throughout your scripts to process errors the way you want.
  • ErrorHandler uses two built-in exception functions, ExceptData and ExceptCalls.
    Except Data
    All built-in exceptions have message text associated with them. ExceptData returns that text.
    ExceptCalls
    Returns a list of the function calls that generated the exception. You can see from ErrorHandler above, that ExceptCalls returns a LIST OF CALL. CALL is a built-in data type that is a record with three elements:
    • sFunction
    • sModule
    • iLine
    ErrorHandler processes each of the calls and prints them in the results file.
  • Silk Test Classic also provides the function ExceptPrint, which combines the features of ExceptCalls, ExceptData, and ExceptNum.

    Testcase VerifyTest - Passed
    *** Error: Verify value failed - got "yyy", expected "xxx"
    Module: Function: Verify Line: 0
    Module: except.t Function: CompValues Line: 121
    Module: except.t Function: VerifyTest Line: 112

    The second line is the result of printing the information from ExceptData. The rest of the lines show the processing of the information from ExceptCalls.

    This test case passes because the error was handled locally and not re-raised.