当前位置:   article > 正文

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

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

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

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

语法

  1. Return From Keyword If ${condition} ${return_value}
  2. ${condition}:这是一个布尔表达式。如果这个条件为真,则关键字将返回。
  3. ${return_value}:这是一个可选的返回值,如果条件为真,将返回这个值。

示例

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

示例 1:简单的使用

robot

  1. *** Keywords ***
  2. Example Keyword
  3. [Arguments] ${value}
  4. Return From Keyword If ${value} == 1 Value is 1
  5. Log Value is not 1
  6. Return From Keyword If ${value} == 2 Value is 2
  7. Log Value is not 2

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

示例 2:带有默认返回值

  1. *** Keywords ***
  2. Check Value
  3. [Arguments] ${value}
  4. Return From Keyword If ${value} > 10 Value is greater than 10
  5. Log Value is less than or equal to 10

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

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

  1. *** Test Cases ***
  2. Test Return From Keyword If
  3. ${result}= Example Keyword 1
  4. Log ${result}
  5. ${result}= Example Keyword 2
  6. Log ${result}
  7. ${result}= Example Keyword 3
  8. Log ${result}
  9. *** Keywords ***
  10. Example Keyword
  11. [Arguments] ${value}
  12. Return From Keyword If ${value} == 1 Value is 1
  13. Log Value is not 1
  14. Return From Keyword If ${value} == 2 Value is 2
  15. Log Value is not 2
  16. [Return] Value is not 1 or 2

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

注意事项

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/710967
推荐阅读
  

闽ICP备14008679号