2017-02-22 80 views
0

我有如下關鍵詞:機器人測試框架:Set Variable如何工作?

App For Port [Arguments] ${app} 
    # Try to launch one app on either port, continue if it fails on port 1 and launch it on port 2, record if it fails on both. 
     ${returned_port} Set Variable ${1} 
    : FOR ${port_num} IN 1 2 
    \ ${returned_port} Set Variable If ${port_num} == 2 ${2} 
    \ Check App Launch ${app} ${port_num} 
    Return From Keyword ${returned_port} 

${returned_port} Set Variable ${1}工作正常,${returned_port}這行之後爲1。

但是, ${returned_port} Set Variable If ${port_num} == 2 ${2}給出${returned_port}沒有值。

我真的很困惑。我怎樣才能讓它設置${returned_port}爲2?提前致謝!!

回答

2

請參閱文檔:http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Variable%20If 「第二個值也可以省略,在這種情況下,它的默認值爲None。」 這意味着Set Variable If在第一次循環迭代期間將${returned_port}設置爲None

除此之外您可以簡化代碼位:

App For Port 
    [Arguments] ${app} 
    : FOR ${port_num} IN 1 2 
    \ Check App Launch ${app} ${port_num} 

...不相同。可以?如果您需要無條件地返回關鍵字的值,請使用[Return] ${var}

還有一 - 你可以考慮使用數據驅動的方法:http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#data-driven-style

+0

感謝,這是有道理的。但是爲了簡化代碼,因爲我需要返回'Check App Launch'的實際用於上層關鍵字的端口,所以我想我需要保持代碼 – Henry

+0

對我有意義。我很高興它有幫助。 –

相關問題