如何查找是否已定义变量? [英] How do I find if a variable has been defined?
问题描述
如何确定我的Robot Framework脚本中是否已定义变量?我正在做API测试,而不是UI测试.我的设置和拆卸过程很复杂,并且由于我正在通过脚本与多台计算机进行交互,因此,如果发生致命错误,了解当前状态非常重要.我可以跟踪一些复杂的元变量集或变量跟踪列表所做的工作,但我更喜欢查询是否已定义了特定变量,如果需要,则采取适当的拆卸步骤.
How do I find out if a variable has been defined in my Robot Framework script? I am doing API testing, not UI testing. I have a complex set up and tear-down sequence and, since I am interacting with multiple computers through the script, it is important to know the current state if a fatal error has occurred. I could track what I have done with some complex set of meta variables or a variable tracking list, but I would prefer to query if a particular variable has been defined and if so take the appropriate tear-down steps.
简化版本如下:
*** Test Cases ***
Check monitor
${monitored}= Connect to Monitor ${Monitor IP Address} ${User name} ${password}
${peer connected}= Connect to Monitor ${Peer IP Address} ${User name} ${password}
Get Information from Monitor ${IP Address}
Send Info to Peer ${buffer1}
Report back to Monitor ${Monitor IP Address}
我们假设拆解会关闭连接.我想关闭所有打开的连接,但是如果我无法打开对等连接,我将关闭监视器连接,并在关闭监视器连接时失败.
We are assuming that the tear-down closes the connections. I want to close any connections that are open, but if I failed to open the peer connection I will close the monitor connection and fail on closing the monitor connection.
我正在尝试确定是否定义了$ {peer connected}.我可以查看Robot Framework的变量存储以查看它是否存在(在该词典中吗?)?
I am trying to determine if ${peer connected} is defined. Can I look into Robot Framework's variable storage to see if it is there (in that dictionary?)?
推荐答案
You can call Get Variables to get a dictionary of all variables, then check whether the variable you're interested in is in the dictionary.
*** Test cases ***
Example
${foo}= set variable hello, world
${variables}= Get variables
Should be true "\${foo}" in $variables
Should not be true "\${bar}" in $variables
这篇关于如何查找是否已定义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!