字符串变量args在机器人框架中被视为dict类型

时间:2015-04-01 17:53:55

标签: python robotframework

*** 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'>

1 个答案:

答案 0 :(得分:3)

您的变量是一个字符串。要检查它,只需尝试在其上执行字典关键字(例如&#34;从字典&#34;从Collections lib)获取,您将看到它失败。

运行此代码以查看它:

*** 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

要理解为什么从评估中获得dict类型,你必须明白evaluate只是&#34;在Python中评估给定的表达式并返回结果。&#34;。所以它把它的参数作为一个普通的字符串用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'>

这是完全正常的,因为&#34;字符串&#34; {&#34; a&#34;:&#34; b&#34;}是用Python声明字典的方式。

所以: