我正在尝试将字典设置为变量(这样我就可以将其用作资源并从另一个文件访问其值),但有一些东西让我发疯。
这是我的代码(仅用于测试目的):
*** Settings ***
Documentation Suite description
Library Collections
*** Variables ***
&{SOME DICT} key1=value1 key2=value2
*** Test Cases ***
Dict Test # why $ instead of &?
${RANDOM VAR}= Get From Dictionary ${SOME DICT} key1
Log ${RANDOM VAR} WARN
Run Code Online (Sandbox Code Playgroud)
如果我运行它,我得到了预期的结果([ WARN ] value1),但是 IDE (PyCharm) 抱怨${SOME DICT}变量未定义,并且字典声明未像变量或列表一样突出显示。
如果我将其更改为&{SOME DICT} IDE 将不再抱怨,但测试失败并显示以下输出:
Dict Test | FAIL |
Keyword 'Collections.Get From Dictionary' got positional argument after named arguments.
Run Code Online (Sandbox Code Playgroud)
这让我很困惑:如果它是一本字典才能使其工作,为什么我必须使用 $ 而不是 & ?是不是我做错了什么,只是运气好?
感谢您的任何建议或指导!
查看“从字典获取”libdoc,看起来示例显示的内容与您的工作片段相同:
Run Code Online (Sandbox Code Playgroud)Name: Get From Dictionary Source: Library (Collections) Arguments: [dictionary, key] Returns a value from the given ``dictionary`` based on the given ``key``. If the given ``key`` cannot be found from the ``dictionary``, this keyword fails. The given dictionary is never altered by this keyword. Example: | ${value} = | Get From Dictionary | ${D3} | b | => | ${value} = 2
关键字实现细节如下:
try:
return dictionary[key]
except KeyError:
raise RuntimeError("Dictionary does not contain key '%s'." % key)
Run Code Online (Sandbox Code Playgroud)
因此,实际上,Robot 发送的是字典内容的表示,而不是字典名称,因此可以返回键的值。这与Python中的直接调用相同:
a = {u'key1': u'value1', u'key2': u'value2'}
print(a['key1'])
Run Code Online (Sandbox Code Playgroud)
最后,该 KW 的 libdoc 并不简单,但您的 Robot 的 PyCharm 插件在这种情况下无法正常工作。在 RED Robot Editor(基于 Eclipse)中,正确的大小写不会在编辑器中出现任何警告,错误的大小写提供有关参数的错误标记(更好,但仍然不清楚到底是什么错误。归咎于简约的 libdoc 信息)。
附:需要澄清的是,我是 RED 项目的负责人。