无法设置 RobotFramework 字典

Cannot set a RobotFramework dictionary

我写了这个简单的最小脚本来展示我的痛苦:

main.robot

Library  Collections
Library          BuiltIn
Library          String


*** Variables ***
&{info}    Create Dictionary


*** Test Cases ***
Case_00_Initialization
    Log     Hello 1     WARN
    Set To Dictionary    ${info}   field1=A sample string
    Log     Hello 2     WARN

运行 此代码由

python -m robot -L TRACE --NoStatusRC main.robot

给我错误:

[ ERROR ] Error in file 'C:\test\main.robot' on line 7: Setting variable '&{info}' failed: Invalid dictionary variable item 'Create Dictionary'. Items must use 'name=value' syntax or be dictionary variables themselves.
==============================================================================
Main
==============================================================================
[ WARN ] Hello 1
Case_00_Initialization                                                | FAIL |
No keyword with name 'Set To Dictionary' found.
------------------------------------------------------------------------------
Main                                                                  | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output:  C:\test\output.xml
Log:     C:\test\log.html
Report:  C:\test\report.html

应用程序应该在初始化时设置一个变量info,它将在下一个测试用例中使用。但是我不想使用 Set Global Variable

请注意,这是一个最小的工作示例,不建议在 Variables 部分设置 field1。这不可能。即使是那个也解决不了 No keyword with name 'Set To Dictionary' found.

的问题

在“变量”部分您不能使用关键字 - 而这正是您在 Create Dictionary 中所做的。
你可以向它添加一些 key:values (比如 "field" ,但你不允许我们这样做;),或者 - 你可以将它初始化为一个空字典(例如 {} in python).后者通过传递特殊值 &{Empty}:

来完成
*** Variables ***
&{info}    &{Empty}

我终于找到问题所在了。我错过了 *** Settings *** 的那一行我无法想象这很重要。遗憾的是 RF 在网上没有很多完整的例子。

这是工作代码:

*** Settings ***
Library  Collections


*** Variables ***
&{info}


*** Test Cases ***
Case_00_Initialization
    Log     Hello 1     WARN
    Set To Dictionary    ${info}   field1=A sample string
    Log     Hello 2     WARN