机器人框架:异常处理

robot framework: exception handling

是否可以处理测试用例的异常?我有 2 种失败要跟踪:测试失败 运行,测试 运行 但收到错误输出。如果我需要引发异常以使我的测试失败,我如何区分这两种失败类型?所以说我有以下内容:

*** Test Cases ***
Case 1
    Login            1.2.3.4    user    pass
    Check Log For    this log line

如果我无法登录,那么 Login 关键字会引发 ExecutionError。如果日志文件不存在,我也会得到一个 ExecutionError。但是如果日志文件确实存在并且该行不在日志中,我应该得到一个 OutputError.

我可能想立即让 ExecutionError 的测试失败,因为这意味着我的测试没有 运行 并且有一些问题需要在环境中或测试中修复案件。但是在 OutputError 上,我可能想继续测试。它可能仅指单个输出,测试可能有价值继续检查其余输出。

如何做到这一点?

机器人有几个处理错误的关键字,比如Run keyword and ignore error可以用来运行另一个可能会失败的关键字。来自文档:

This keyword returns two values, so that the first is either string PASS or FAIL, depending on the status of the executed keyword. The second value is either the return value of the keyword or the received error message. See Run Keyword And Return Status If you are only interested in the execution status.

话虽这么说,编写一个调用您的登录关键字的基于 python 的关键字可能会更容易,因为它会更容易处理多个异常。

你可以使用这样的东西

${err_msg}=  Run Keyword And Expect Error  *  <Your keyword>
Should Not Be Empty  ${err_msg}

您可以尝试几种不同的变体 Run Keyword And Continue On FailureRun Keyword And Expect ErrorRun Keyword And Ignore Error 用于上面的第一个语句。

上面第二个语句的选项是Should Be Equal As Strings, Should Contain, Should Match.

您可以在 Robot keywords

上探索更多