在Robot Framework关键字中返回结果?

时间:2011-09-28 08:20:23

标签: robotframework

如何在运行关键字后返回结果?

示例:

mykey word [Arguments] input
   ${results}=  getme input

但我想使用这些结果:

 ${results} = mykey word  newinput

5 个答案:

答案 0 :(得分:28)

Robot Framework用户指南介绍了如何从关键字返回值。请参阅 User keyword return values

简短版本是:在关键字中设置变量,并使用[return]测试用例设置返回该变量。

示例,使用以管道分隔的纯文本格式:

*** Keywords ***
| mykey word
| | [Arguments] | ${input}
| | ${string}= | set variable | the string is "${input}"
| | [return] | ${string}

*** Test Cases ***
| Call custom keyword and get result
| | ${results}= | mykey word | newinput
| | log | ${results}

答案 1 :(得分:11)

一个简单的例子可能会有所帮助:

*** Keywords ***
Convert temperature F To Centigrade
  [Arguments]  ${ftemp}
  ${ftemp} =  Convert To Float  ${ftemp}
  ${ctemp} =  ${0.9} * ${ftemp} - ${32}
  [Return]  ${ctemp}

Convert temperature C To Fahrenheit
  [Arguments]  ${ctemp}
  ${ctemp} =  Convert To Float  ${ctemp}
  ${ftemp} =  ${1.8} * ${ctemp} + ${32}
  [Return]  ${ftemp}

*** Test Cases ***
Verify Temperature Conversion  
  ${result} =  Convert temperature F To Centigrade  ${32}
  Should Be Equal  ${result}  ${0}
  ${result} =  Convert temperature C To Fahrenheit  ${0}
  Should Be Equal  ${result}  ${32}

答案 2 :(得分:2)

使用[Return]返回结果。

一个例子是:

Time Stamp

      [Return]  ${time_stamp}
      ${secs}=  Get Time  epoch
      ${time}=  Get Time
      ${time_stamp}=  Convert To String      ${secs}

$ {time_stamp}的值将存储在Time Stamp关键字中。

答案 3 :(得分:1)

最简单的方法是使用关键字末尾的建议[返回]标记,但存在其他方式。

使用关键字Set Global Variable,您可以在其运行的关键字之外访问变量,而无需从关键字本身返回任何内容。如果你想避免使你的主变量列表混乱并且在后台有一些变量,那么这很有用,但是要像使用任何全局变量一样谨慎使用它。

答案 4 :(得分:0)

# This example will explain the usage of build in library keywords.
# The "Evaluate", "Log", and "Return" settings by using Fahrenheit to Centigrade
# conversion logic on the variable ${var1}.

*** Variables ***
${var1}     32
*** Keywords ***
Convert temperature Fahrenheit To Centigrade
  [Arguments]  ${ftemp}
  ${ftemp} =  Convert To Number     ${ftemp}
  ${ctemp} =  evaluate  (5 * (${ftemp} - 32))/9
  [Return]  ${ctemp}


*** Test Cases ***
Verify Temperature Conversion F to C
  ${result} =  Convert temperature Fahrenheit To Centigrade  ${var1}
  Log  ${result}
  Should Be Equal As Numbers    ${result}   0.0