Robotframework 获取重复的迭代

Robotframework get iteration for repeat

我在 robotframework 中使用重复调用关键字 x 次。 我想要的是获取调用的关键字中的迭代次数。

我尝试了什么:

${iteration} =          0

*** Test Cases ***
Start Test
    repeat keyword  ${duration}  Run test iteration ${iteration}

*** Keywords ***
Run test iteration ${loop}
    ${iteration}=  Set Variable  evaluate  ${iteration}+1

我期望的是在报告中被调用的关键字如下所示:

Run test iteration 0
Run test iteration 1
Run test iteration 2

但我明白了

Run test iteration 0
Run test iteration 0
Run test iteration 0

有没有可能得到我预测的结果?

使用 robotframework 3.1.1

是的,这一切都归结为变量范围。
当您在关键字中进行赋值时,该变量的作用域为关键字的局部范围 - 它在其中可见(而不是调用它的潜在关键字),并在关键字完成时被删除。这是不管它与变量部分中定义的名称相同的名称 - 您实际上是在重新定义一个具有相同名称的新名称,例如覆盖它。并且不以任何方式改变“全局”的值。
然后在下一个循环中重复此操作,因此您在每个 运行.

中得到一个 0

修复很简单 - 在更改值之后,声明您希望此变量在更高范围(高于当前关键字)中可见。您可以选择 3 - 案例级别、套件或全局(因此所有后续套件和案例都可以使用它);这是案例级别:

Run test iteration ${loop}
    ${iteration}=  Evaluate  ${iteration}+1
    Set Test Variable    ${iteration}