10、有条件提前退出关键字Return From Keyword If【robot framework】

在 Robot Framework 中,Return From Keyword If 是一个有用的关键字,它允许你在特定条件下从关键字中返回。这在需要在满足某个条件时提前退出关键字的情况下特别有用。

以下是 Return From Keyword If 的语法和使用示例:

语法

Return From Keyword If    ${condition}    ${return_value}


    ${condition}:这是一个布尔表达式。如果这个条件为真,则关键字将返回。

    ${return_value}:这是一个可选的返回值,如果条件为真,将返回这个值。

示例

以下是一些使用 Return From Keyword If 的示例:

示例 1:简单的使用

robot

*** Keywords ***

Example Keyword

    [Arguments]  ${value}

    Return From Keyword If    ${value} == 1    Value is 1

    Log  Value is not 1

    Return From Keyword If    ${value} == 2    Value is 2

    Log  Value is not 2

在这个示例中,如果传递的 value 是 1 或 2,关键字将提前返回,并且不会执行后续的 Log 语句。

示例 2:带有默认返回值

*** Keywords ***

Check Value

    [Arguments]  ${value}

    Return From Keyword If    ${value} > 10    Value is greater than 10

    Log  Value is less than or equal to 10

在这个示例中,如果 value 大于 10,关键字将提前返回字符串 "Value is greater than 10"。否则,将继续执行 Log 语句。

示例 3:在测试用例中使用

*** Test Cases ***

Test Return From Keyword If

    ${result}=  Example Keyword  1

    Log  ${result}

    ${result}=  Example Keyword  2

    Log  ${result}

    ${result}=  Example Keyword  3

    Log  ${result}


*** Keywords ***

Example Keyword

    [Arguments]  ${value}

    Return From Keyword If    ${value} == 1    Value is 1

    Log  Value is not 1

    Return From Keyword If    ${value} == 2    Value is 2

    Log  Value is not 2

    [Return]  Value is not 1 or 2

在这个示例中,测试用例调用了 Example Keyword 三次,分别传入不同的值,并将结果记录下来。根据传入的值,Example Keyword 将返回相应的结果。

注意事项

    Return From Keyword If 只能在关键字内部使用,不能在测试用例中直接使用。

    它使得关键字可以根据条件提前返回,从而避免不必要的执行。

通过使用 Return From Keyword If,你可以使关键字更加灵活和易于维护,因为它们可以根据条件在不同点提前退出。

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值