2015-04-01 33 views
0
*** 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 ,機器人版畫運行此,字符串變量ARG遊戲當作字典類型的機器人框架

the type object is <type 'dict'> 

但是,是不是默認的行爲治療單引號文字爲strings?所以,理想情況下它必須打印<type 'str'>而不是<type 'dict'>

+0

我猜python解釋器可以解析參數並推斷它們的類型嗎? – Ashalynd 2015-04-01 17:59:52

+1

可能,你是對的..當我通過'-v TC_ARGS:'abc''時,它會拋出錯誤提示'變量abc沒有被定義在任何地方?所以,我認爲,當我們在機器人字符串上執行「評估」時會看到這個問題。 Python'evaluate'將機器人字符串視爲變量。 – 2015-04-01 18:04:14

+0

是的,我認爲你是對的。試試'-v TC_ARGS:'「abc」'',那麼它可能是一個字符串。 – Ashalynd 2015-04-01 18:23:18

回答

3

你的變量是一個字符串。要檢查它,只是試着做一本字典的關鍵字就可以了(比如從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 

要理解爲什麼你得到字典類型的評估,你必須明白,evaluate只是「在Python中評估給定的表達式並返回結果」。因此,它將它的參數作爲一個純字符串與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中聲明一個字典。

所以: