如何在RobotFramework中使用具有默认值的用户关键字参数

A20*_*Tom 7 robotframework

有人可以解释一下如何使用与数据驱动的测试用例相关的可选关键字参数的默认值吗?

正如您在我的示例中所看到的,在某些情况下不会使用所有默认值:

*** Test Cases ***
| testArgs | [Template] | doSomething
| | 111 : 222 : 333 : 444
| | xxx : 222 : 333 : 444 | xxx
| | 111 : xxx : 333 : 444 | | xxx
| | 111 : xxx : 333 : 444 | ${EMPTY} | xxx
| | None : xxx : 333 : 444 | ${None} | xxx
| | None : xxx : 333 : 444 | ${null} | xxx
| | 111 : 222 : xxx : 444  | | | xxx

*** Keywords ***
| doSomething
| | [Arguments] | ${expected} | ${arg1}=111 | ${arg2}=222 | ${arg3}=333 | ${arg4}=444
| | Log | exp: ${expected}
| | ${rc} | Set Variable | ${arg1} : ${arg2} : ${arg3} : ${arg4}
| | Log | arg: ${rc}
| | Run Keyword If  | '${rc}' == '${expected}'  
| | ... | Log | === equal ===
| | ... | ELSE  
| | ... | Log | !!! diff !!!
| | Log | **************************
| | Should be equal | ${rc} | ${expected}
Run Code Online (Sandbox Code Playgroud)

结果:

testArgs                                                              | FAIL |
Several failures occurred:
1)  : xxx : 333 : 444 != 111 : xxx : 333 : 444
2)  : xxx : 333 : 444 != 111 : xxx : 333 : 444
3)  :  : xxx : 444 != 111 : 222 : xxx : 444
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用命名参数来设置特定的关键字参数.但这只能在关键字驱动的测试用例中实现.

问候,汤姆

小智 12

仅使用您要传递的参数调用特定关键字.例如:

*** Test Cases ***
TEST
    MyKeyword   a=1    c=3

*** Keywords ***
MyKeywords
    [Arguments]   ${a}=0   ${b}=2   ${c}=3
Run Code Online (Sandbox Code Playgroud)

在这里,我没有传递参数'b'.默认情况下,它将需要b = 2


小智 5

《 Robot Framework用户指南》中的示例:

*** Keywords ***
    Two Arguments With Defaults
    [Arguments]    ${arg1}=default 1    ${arg2}=${VARIABLE}
    [Documentation]    This keyword takes 0-2 arguments
    Log    1st argument ${arg1}
    Log    2nd argument ${arg2}

 *** Test Cases ***
Example
    Two Arguments With Defaults    arg2=new value
Run Code Online (Sandbox Code Playgroud)

所以彼得·宾厄姆(Peter Bingham)是对的:

*** Test Cases ***
TEST
    MyKeyword   a=1    c=3

*** Keywords ***
MyKeywords
    [Arguments]   ${a}=0   ${b}=2   ${c}=3
Run Code Online (Sandbox Code Playgroud)