【发布时间】:2020-05-17 03:09:16
【问题描述】:
考虑以下代码:
在 Utils.py 中:
@keyword
def get_compound_dictionary():
"""
https://docs.python.org/3/library/copy.html
An example compound dictionary
"""
return {'key1': 'value1', 'deep_dict': {'key2': 'value2'}}
在 collection-library-tests.robot 中
*** Settings ***
Documentation A test suite utilizing all collection library keywords
Library Collections
Library Utils.py
# To run:
# robot --pythonpath Resources --noncritical failure-expected -d Results/ Tests/collection-
library-tests.robot
*** Test Cases ***
Use "Copy Dictionary" : Shallow Copy
${compound_python_dictionary} = get compound dictionary
&{shallow_copy} = Copy Dictionary ${compound_python_dictionary} deepcopy=False
# if we modify the contained objects (i.e. deep_dict) through the shallow_copy,
# the original compound_python_dictionary will see the changes in the contained objects
Set To Dictionary ${shallow_copy}[deep_dict] key2=modified
Log ${shallow_copy}
Log ${compound_python_dictionary}
Should Be Equal ${compound_python_dictionary}[deep_dict][key2] modified # fails, why?
目标在测试用例中表述为:
如果我们通过 shallow_copy 修改包含的对象(即 deep_dict), 原来的 Compound_python_dictionary 会看到所包含对象的变化
预期结果
Should Be Equal ${compound_python_dictionary}[deep_dict][key2] modified # passes
请注意,我使用的是 Robot FW 版本:Robot Framework 3.1.2 (Python 3.7.4 在 Linux 上)
Acc.to the documentation about Copy Dictionary:
deepcopy 参数控制返回的字典是否应该是 浅拷贝或深拷贝。默认情况下返回一个浅拷贝,但这可以是 通过给 deepcopy 一个真值来改变(参见布尔参数)。这 > 是 Robot Framework 3.1.2 中的一个新选项。早期版本总是 返回浅拷贝。
Acc.to the documentation about Boolean Arguments:
一些关键字接受作为布尔值 true 或 false 处理的参数。如果这样的参数以字符串形式给出,则如果它是空字符串或等于 FALSE、NONE、NO、OFF 或 0,则视为 false,不区分大小写。其他字符串无论其值如何都被视为真。
另请注意,我也尝试了deepcopy=${False},产生了相同的观察结果。
【问题讨论】:
标签: robotframework