robotframework的FOR LOOP有2中格式

Robot Framework 3.1版本对FOR LOOP进行了增强,包括以END关键字明确结束循环,取消强制缩进,简化了语法。此外,还介绍了新旧两种格式的区别,并提醒用户在未来的版本中将废弃旧格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在最前面:

FOR LOOP有2中格式,在3.1版本之前仅支持旧格式,3.1及之后能支持2中格式

 

RF的文档可以从此处获取,各个版本都有:

http://robotframework.org/robotframework/

 

旧格式:

:FOR    ${item}    IN   @{bugs}
\    ${all}=    Set Variable    <a href="${item['url']}">${item['key']}</a> ${all}

新格式:

FOR    ${item}    IN   @{bugs}
    ${all}=    Set Variable    <a href="${item['url']}">${item['key']}</a> ${all}
END


# 也可以不用缩进
FOR    ${item}    IN   @{bugs}
${all}=    Set Variable    <a href="${item['url']}">${item['key']}</a> ${all}
END

3.1文档中的原文摘抄如下:

For loop syntax was enhanced in various ways in Robot Framework 3.1. The most noticeable change was that loops nowadays end with the explicit END marker and keywords inside the loop do not need to be indented. In the space separated plain text format indentation required escaping with a backslash which resulted in quite ugly syntax:

*** Test Cases ***
Example
    :FOR    ${animal}    IN    cat    dog
    \    Log    ${animal}
    \    Log    2nd keyword
    Log    Outside loop

Another change, also visible in the example above, was that the for loop marker used to be :FOR when nowadays just FOR is enough. Related to that, the :FOR marker and also the IN separator were case-insensitive but nowadays both FOR and IN are case-sensitive.

Old for loop syntax still works in Robot Framework 3.1 and only using IN case-insensitively causes a deprecation warning. Not closing loops with END, escaping keywords inside loops with \, and using :FOR instead of FOR are all going to be deprecated in Robot Framework 3.2. Users are advised to switch to the new syntax as soon as possible.

简单翻译下:

在RF3.1中对FOR LOOP进行了加强。其中最显著的变化是,新版的FOR LOOD是以一个关键字 END 来表示结束的,且循环体内的关键字不用在用空格进行缩进了(不要空格缩进,也就不需要使用\来进行转义了,这个转义语法太恶心了)

另外一个变化是,旧版的 :FOR被FOR所代替,不再需要哪个前置的冒号了。 同时,在旧版中 :FOR 和 IN 都是不区分大小写的,但是新版中的 FOR 和 IN 都是区分大小写的,这个要注意。

在RF3.1中,旧版的FOR LOOP还能使用,但是如果使用旧版时没有用大写,会有一个warning(Using 'in' as a FOR loop separator is deprecated. Use 'IN' instead.)。 旧格式在将来的3.2版本中,将不再建议使用,希望大家尽快使用新的格式。

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值