首页 > 解决方案 > 无法设置 RobotFramework 字典

问题描述

我写了这个简单的最小脚本来展示我是如何受苦的:

主机器人

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

请注意,这是一个最小的工作示例,不建议field1Variables部分设置。这不可能。连那个也解决不了问题No keyword with name 'Set To Dictionary' found

标签: robotframework

解决方案


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

*** Variables ***
&{info}    &{Empty}

推荐阅读