Python Robot从关键字使用中返回无效

时间:2016-06-02 07:09:04

标签: python robotframework

我正在尝试在Robot中执行以下代码并获得以下错误。 我很难弄清楚什么是错的

${job_id}=    Run Keyword And Return If    "${cur_build}" != "<my_Build>"    perform software upgrade    ${wd}    <device_name>    <my_build>

下面是xml输出。

Success:  0   Failed :  0   Skipped :  0
 Stop  Delete</msg>
<status status="PASS" endtime="20160602 02:22:51.590" starttime="20160602 02:22:10.415"></status>
</kw>
<msg timestamp="20160602 02:22:51.591" level="INFO">Returning from the enclosing user keyword.</msg>
<status status="PASS" endtime="20160602 02:22:51.591" starttime="20160602 02:22:10.413"></status>

</kw>
<tags>
<tag>draft</tag>
<tag>upgrade</tag>
</tags>
<status status="FAIL" endtime="20160602 02:22:51.592" critical="yes" starttime="20160602 02:21:01.560">Invalid 'Return From Keyword' usage.</status>

修改

所以我理解Run Keyword和Return if只能在关键字中使用而不能在Testcase中使用。但是,我将如何在测试用例中执行类似下面的操作

if <some_cond>:
   ${my_var}=    <Keyword A>
   Keyword B=    ${my_var}

一种方法可能是添加定义新关键字,但有没有办法在测试用例中执行此操作而不定义任何新关键字

1 个答案:

答案 0 :(得分:0)

如果以下代码符合您的目的,请告诉我

*** Variables ***
@{list1}          @{EMPTY}
@{list2}          This    is    list    list1

*** Test Cases ***
Conditional Test Case1
    ${flag}=    Set Variable If    @{list1}!=@{EMPTY}    ${true}    ${false}
    ${rc}=    Run Keyword If    ${flag}==${true}    Log Multiple Values    ${list1}
    ${rc}=    Run Keyword Unless    ${flag}==${false}    Log Multiple Values    ${rc}

Contitional Test Case2
    ${flag}=    Evaluate    len(${list2}) is not None
    ${rc}=    Run Keyword If    ${flag} == ${true}    Log Multiple Values    ${list2}

*** Keywords ***
Log Multiple Values
    [Arguments]    @{list_args}
    [Documentation]    This keyword logs its arguments and also returns them
    Log Many    @{list_args}
    [Return]    @{list_args}