当前位置: 首页 > news >正文

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 1Log  Value is not 1Return From Keyword If    ${value} == 2    Value is 2Log  Value is not 2

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

示例 2:带有默认返回值

*** Keywords ***Check Value[Arguments]  ${value}Return From Keyword If    ${value} > 10    Value is greater than 10Log  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  1Log  ${result}${result}=  Example Keyword  2Log  ${result}${result}=  Example Keyword  3Log  ${result}*** Keywords ***Example Keyword[Arguments]  ${value}Return From Keyword If    ${value} == 1    Value is 1Log  Value is not 1Return From Keyword If    ${value} == 2    Value is 2Log  Value is not 2[Return]  Value is not 1 or 2

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

注意事项

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

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

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

http://www.lryc.cn/news/366440.html

相关文章:

  • JAVA开发的一套(智造制造领航者云MES系统成品源码)saas云MES制造执行系统源码,全套源码,支持二次开发
  • 探究JSON和XML:两种常见的数据交换格式之异同
  • SQL查询的优化方案
  • 【C语言题解】1、写一个宏来计算结构体中某成员相对于首地址的偏移量;2、写一个宏来交换一个整数二进制的奇偶位
  • LabVIEW阀性能试验台测控系统
  • Flutter 中的 LayoutBuilder 小部件:全面指南
  • webman中创建udp服务
  • Vue 学习笔记 总结
  • 云计算导论(3)---分布式文件系统
  • 后端进阶-分库分表
  • Apple开发者应用商店(AppStore)描述文件及ADHOC描述文件生成
  • 【Git】修改设置 git 的 username、email
  • python脚本打包为exe并在服务器上设置定时执行
  • STM32-呼吸灯仿真
  • 【AI基础】第三步:纯天然保姆喂饭级-安装并运行chatglm2-6b
  • Spring Security系列之Handler
  • Thinkphp使用Elasticsearch查询
  • 开源日志分析平台ELK实战应用
  • css基本操作及使用
  • SpringBoot+Vue幼儿园管理系统(前后端分离)
  • MFC实现子控件focus焦点上下移动父控件ListView和Gridview也跟着向上下移动
  • 几何关系运算处理
  • http和https分别是什么?区别是什么?
  • 第一周:计算机网络概述(上)
  • 谷歌AI搜索变革,中国引擎能跟上步伐?
  • 【机器学习300问】110、什么是Lasso回归模型?
  • Qt实现麦克风音频输入保存wav文件
  • docker_如何推送镜像到仓库(hub.docker.com)
  • 【Python】认识 Python
  • Vue根据后端返回的tabList动态渲染组件信息