python - Why dont I have the modified dictionary in Robot, even though the Python keyword has modified the dictionary?
问题描述
Here is my Robot File (robot-dictionary-tests.robot)
*** Settings ***
Documentation Passing a dictionary object from Robot to Python and vice versa
Library Utils.py
# To run:
# robot -L debug -d Results/ Tests/robot-dictionary-tests.robot
*** Keywords ***
Call Python To Modify The Dictionary
[arguments] &{named}
modify dictionary ${named}
*** Test Cases ***
Test Modifying Robot Dictionary in Python
&{d} = Create Dictionary key=original
&{expected} = Create Dictionary key=modified
Call Python To Modify The Dictionary &{d}
Should Be Equal ${d} ${expected}
Here is my Utils.py:
from robot.api import logger
@keyword('modify dictionary')
def modify_dictionary(d):
logger.debug(f'before modification, d: {d}')
for key in d:
d[key] = 'modified'
logger.debug(f'after modification, d: {d}')
The Test Case Test Modifying Robot Dictionary in Python fails:
My Question
Why does the robot test fail even though I passed the dictionary d reference to @keyword('modify dictionary')?
解决方案
As you've suspected, the variables are by reference and thus changed. To my knowledge this has more to do with Python, then Robot Framework.
Passing a dictionary as an object should be done through ${VarName} and not &{VarName}. The documenation on Dictionary Variables explains this like:
...a Python dictionary or a dictionary-like object can be used as a dictionary variable like &{EXAMPLE}. In practice this means that individual items of the dictionary are passed as named arguments to the keyword
In the below code for keyword Call Python To Modify The Dictionary
I change the keyword argument &{named}
to ${named}
and where the keyword is called &{d}
to ${d}
.
*** Keywords ***
Call Python To Modify The Dictionary
[arguments] ${named}
modify dictionary ${named}
*** Test Cases ***
Test Modifying Robot Dictionary in Python
&{d} = Create Dictionary key=original
&{expected} = Create Dictionary key=modified
Call Python To Modify The Dictionary ${d}
Should Be Equal ${d} ${expected}
Resulting in:
Suite Executor: Robot Framework 3.1.2 (Python 3.7.0 on win32)
==============================================================================
SO003
==============================================================================
SO003.Test :: Passing a dictionary object from Robot to Python and vice versa
==============================================================================
Test Modifying Robot Dictionary in Python | PASS |
------------------------------------------------------------------------------
SO003.Test :: Passing a dictionary object from Robot to Python and... | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
SO003 | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
推荐阅读
- r - 更改 RMarkdown pdf 输出中图形标题的字体大小
- sql - Azure SQL 删除性能比本地 SQL-Express 服务器慢很多
- react-native - 如何将 oAuth 会话令牌从 salesforce react native 应用程序传递给 react-native webview?
- r - 在 R 中启动 h2o 挂起
- c# - TagHelper ModelExpression 元数据中不存在描述属性文本
- javascript - 加入来自不同来源的 HTML 段并在浏览器中正确加载
- python - 将 StringVar 绑定到 Entry 小部件
- jooq - JOOQ - 如何将 array_agg 与自定义类型字段一起使用
- bash - 如何在图案和以另一个图案开头的线条之间打印线条?
- react-native - 通过 react-native 访问移动设备的真实世界倾斜度和方向