字符串变量 args 在机器人框架中被视为字典类型
String variable args are treated as dict types in robot framework
*** Test Cases ***
Log Test
Run Keyword LogType
*** Keyword ***
LogType
${type_object}= Evaluate type( ${TC_ARGS} )
Log To Console the type object is ${type_object}
当我 运行 使用命令时,pybot -v TC_ARGS:'{"a":"b"}' a.robot
,机器人打印,
the type object is <type 'dict'>
但是,将单引号文字视为 strings
不是默认行为吗?因此,理想情况下它必须打印 <type 'str'>
而不是 <type 'dict'>
。
您的变量是一个字符串。要检查它,只需尝试在它上面做一个字典关键字(比如 Collections lib) 中的 "get from dictionary",你会看到它失败了。
运行这段代码看看吧:
*** Settings ***
Library Collections
*** Test Cases ***
Log Test
# let's test a proper dictionary
${dict} = create dictionary a b
${value} = get from dictionary ${dict} a
should be equal ${value} b
log to console ${\n}this was a real dictionary
# ${TC_ARGS} is passed on command line
# evaluate might let us thing we have a dict
${type_object} = Evaluate type( ${TC_ARGS} )
Log To Console the type object is ${type_object}
# but in fact this is a string and doing a dict operation will fail
get from dictionary ${type_object} a
要理解为什么从 evaluate 中得到 dict 类型,你必须理解 evaluate 就是 "evaluating the given expression in Python and returns the results."。所以它将它的参数作为一个纯字符串,用 Python 启动它,就像你在 Python 命令行上做的那样。
现在,如果您在 Python 命令行上检查参数,您会得到以下结果:
$ python
Python 2.7.9 (default, Dec 19 2014, 06:00:59)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> type({"a":"b"})
<type 'dict'>
这是完全正常的,因为 "string" {"a":"b"} 是您在 Python.
中声明字典的方式
所以:
- 小心"evaluate"
- 如果您需要从 Robot 命令行传递字典,您会将其作为字符串传递,然后 loads it with json Python library。
*** Test Cases ***
Log Test
Run Keyword LogType
*** Keyword ***
LogType
${type_object}= Evaluate type( ${TC_ARGS} )
Log To Console the type object is ${type_object}
当我 运行 使用命令时,pybot -v TC_ARGS:'{"a":"b"}' a.robot
,机器人打印,
the type object is <type 'dict'>
但是,将单引号文字视为 strings
不是默认行为吗?因此,理想情况下它必须打印 <type 'str'>
而不是 <type 'dict'>
。
您的变量是一个字符串。要检查它,只需尝试在它上面做一个字典关键字(比如 Collections lib) 中的 "get from dictionary",你会看到它失败了。
运行这段代码看看吧:
*** Settings ***
Library Collections
*** Test Cases ***
Log Test
# let's test a proper dictionary
${dict} = create dictionary a b
${value} = get from dictionary ${dict} a
should be equal ${value} b
log to console ${\n}this was a real dictionary
# ${TC_ARGS} is passed on command line
# evaluate might let us thing we have a dict
${type_object} = Evaluate type( ${TC_ARGS} )
Log To Console the type object is ${type_object}
# but in fact this is a string and doing a dict operation will fail
get from dictionary ${type_object} a
要理解为什么从 evaluate 中得到 dict 类型,你必须理解 evaluate 就是 "evaluating the given expression in Python and returns the results."。所以它将它的参数作为一个纯字符串,用 Python 启动它,就像你在 Python 命令行上做的那样。
现在,如果您在 Python 命令行上检查参数,您会得到以下结果:
$ python
Python 2.7.9 (default, Dec 19 2014, 06:00:59)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> type({"a":"b"})
<type 'dict'>
这是完全正常的,因为 "string" {"a":"b"} 是您在 Python.
中声明字典的方式所以:
- 小心"evaluate"
- 如果您需要从 Robot 命令行传递字典,您会将其作为字符串传递,然后 loads it with json Python library。