这是我的机器人文件(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}
这是我的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}')
我的问题
即使我将字典d引用传递给@keyword('modify dictionary'),为何机器人测试仍会失败?
正如您所怀疑的那样,变量是通过引用进行更改的。据我所知,这更多与Python有关,而与Robot Framework有关。
将字典作为对象传递应该通过$ {VarName}而不是&{VarName}完成。关于字典变量的文档解释如下:
... Python字典或类似字典的对象可用作&{EXAMPLE}之类的字典变量。实际上,这意味着将字典中的各个项目作为命名参数传递给关键字
在下面的代码关键字Call Python To Modify The Dictionary
更改关键字参数&{named}
来${named}
而且关键字被调用&{d}
到${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}
导致:
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
==============================================================================
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句