[原创]SilkTest(4Test语言)的异常处理(Exception process in SilkTest)--中英对照翻译

上一篇 / 下一篇  2011-01-26 04:30:56 / 个人分类:原创翻译

Suppose there is a testcase which has 3-verification functions in each verification function there is Do Except error handling statement.
假设一个testcase 有3个验证功能, 每一个检查功能有Do Except错误处理表达式。

In this case if first verification function fails, it will log the result to result file. the rest two verification functions will execute without aborting the test case.
so once the test case executes completely, user can see the result file and come to the conclusion which verification function got failed.This is one use of exception handling.
这样的情况下,如果第一个验证功能失败,它就会纪录错误结果到result文件内。另外两个验证功能会无需中断而被执行。因此一旦testcase被执行完毕,用户能够看到resule文件,然后得到验证功能失败的结论。这就是使用异常处理的一个使用方式。

Using do...except statements to trap and handle exceptions
使用do…except表达式来设计和处理异常
Using do...except you can handle exceptions locally, instead of passing control to SilkTest’s built-in error handler (which is part of the recovery system). The statement has the following syntax:
使用do…except, 你能够替代通过silktest内置错误控件(recovery system恢复系统的一部分)来处理本例异常. 下面是表达式的公式:
do
      statements 表达式
except
      statements 表达式

If an exception is raised in the do clause of the statement, control is immediately passed to the except clause, instead of to the recovery system.
If no exception is raised in the do clause of the statement, control is passed to the line after the except clause.  The statements in the except clause are not executed.
Consider this simple testcase:
如果一个异常是在do语句中被发现,控件就立刻被递解到异常语句,而不是递解到recovery恢复系统. 如果在do语句中没有异常,控件就把此行传递到except语句后面。 Except语句表达式就不被执行。

testcase except1 (STRING sExpectedVal, STRING sActualVal)
    do
        Verify (sExpectedVal, sActualVal)
        Print ("Verification succeeded验证成功")
    except
        Print ("Verification failed验证失败")

This testcase uses the built-in function Verify, which generates an exception if its two arguments are not equivalent. In this testcase, if sExpectedVal equals sActualVal, no exception is raised, "Verification succeeded" is printed, and the testcase terminates. If the two values are not equal, Verify raises an exception, control immediately passes to the except clause (the first Print statement is not executed), and "Verification failed" is printed.
这个testcase使用了内置功能来验证,如果2个arguments不是同等的,它就产生异常。在此testcase中,如果sExpectedVal 等于 sActualVal,就没有异常发现, "Verification succeeded验证成功"就被打印出来, 同时testcase也被中断。如果两个变量不一样,验证发现了一个异常,控件就会立即被传递到except语句(第一个打印表达式不会被执行),同时"Verification succeeded验证成功"被打印出来。

Here is the result if the two values "one" and "two" are passed to the testcase:
这里是如果两个变量“one”和”two”被传递到testcase的结果

Testcase except1 ("one", "two") – Passed通过
    Verification failed 失败

Note that testcase passes and the recovery system is not called, because you handled the error yourself.
See Adding to the default error handling to learn how you can handle the error yourself and call the recovery system too.
You handle the error in the except clause. You can include any 4Test statements, so you could, for example, choose to ignore the error, write information to a separate log file, log the error in the results file, and so on. For some typical uses of do...except, see:
注意,那个testcase通过以后,因为你自己处理了错误的缘故,recovery system恢复系统就不会被调用。 你也可以看到加入默认错误处理方式来学会自己如何处理错误,并且调用recover system恢复系统。
你用except语句中处理了错误,你也可以用任何4stest表达式处理,例如,选择忽略错误,把信息写入到离另外的一个log文件中,在result文件中记录错误等等。下面举例标准的使用do…except的例子:
命令:
do 命令
do
exception raised here此处发现异常
except
statements 表达式
....
control passes 控件通过
....

例子:
testcase except1(STRING sExpectedVal STRING sActualVal)
do
Verify(sExpectedVal sActualVal)
Print(“Verification succeeded验证成功” )
except
Print(“Verification Failed验证失败” )
Raise 命令
In addition to use build-in exceptions you can define your own exceptions and generate them using the raise statement
另外使用内置异常的话,你可以定义你自己的异常,并且产生和使用raise表达式
Reraise 命令

You can also use do...except to perform. some custom error handling then use the reraise statement to pass control to te recovery system as usual
你可以使用do…excaption来执行一些指定义的错误处理,然后和平时一样使用reraise表达式来传递控件到recovery system恢复系统。

 


TAG:

 

评分:0

我来说两句

Open Toolbar