2016-04-28 8 views
1

ロボットフレームワークでテストケースを作成しようとしています。ロボットフレームワークの関数とJson結果を比較する

私たちは結果として私たちにJsonを返す残りの2つのapiを持っています。そのような場合を起動するには、我々はファイル出力の内部JSONを保存rest2.py

def restJSON(): 
    r = requests.get("http://httpbin.org/get") 
# print "Code" , r.status_code 
# print "Text " , r.text 
    return r.json() 

に次のコードを使用していました。そして、我々は

*** Settings *** 
Library rest2.py 
Library OperatingSystem 
*** Test Cases *** 
Example that calls a python keyword 
    ${result}= restJSON 
    ${json}= Get file output 
    Should be equal ${result} ${json} 

以下のようにJSONの比較を評価するために、ロボットのテストケースを書かれている。しかし、我々はpybotのテストケースを実行すると、我々は両方のJSONが同じではないというエラーを取得しています。

-------------------------------- 
pybot testSuite.txt 
-------------------------------- 
============================================================================== 
testSuite 
============================================================================== 
Example that calls a python keyword         | FAIL | 
{u'origin': u'10.252.30.94, 69.241.25.16', u'headers': {u'Via': u'1.1 localhost (squid/3.1.14)', u'Accept-Encoding': u'gzip, deflate, compress', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.2.1 CPython/2.7.6 Linux/3.16.0-30-generic', u'Host': u'httpbin.org', u'Cache-Control': u'max-age=259200'}, u'args': {}, u'url': u'http://httpbin.org/get'} != {u'origin': u'10.252.30.94, 69.241.25.16', u'headers': {u'Via': u'1.1 localhost (squid/3.1.14)', u'Accept-Encoding': u'gzip, deflate, compress', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.2.1 CPython/2.7.6 Linux/3.16.0-30-generic', u'Host': u'httpbin.org', u'Cache-Control': u'max-age=259200'}, u'args': {}, u'url': u'http://httpbin.org/get'} 
------------------------------------------------------------------------------ 
testSuite                | FAIL | 
1 critical test, 0 passed, 1 failed 
1 test total, 0 passed, 1 failed 
============================================================================== 

jsonファイルは同じです。しかし、まだ失敗している両方のテストケースは同じではありません。これを比較する正しい方法か、それともロボットフレームワークで他の方法を使用する方法がありますか?

答えて

1

あなたが行うときの応答は何である:

Should Be Equal As Strings ${result} ${json} 

私は私があなただったらあなたは少し奇妙に何をしたい、私はHTTPライブラリを使用したり、ライブラリ

を要求されるだろうを達成するためのあなたの方法を見つけます

エクサ:なぜ、既存のライブラリを使用していない

https://github.com/peritus/robotframework-httplibrary https://github.com/bulkan/robotframework-requests

1

mpleコード:

pip install robotframework-requests 
pip install requests 

*** Settings *** 
Library      RequestsLibrary 
Library      Collections 
Library      XML use_lxml=True 
Force Tags     REST 


*** Variables *** 
${SERVICE_ROOT} http://ip.jsontest.com/ 
${SERVICE_NAME} testing 

*** Keywords *** 
json_property_should_equal 
    [Arguments] ${json} ${property} ${value_expected} 
    ${value_found} = Get From Dictionary ${json} ${property} 
    ${error_message} = Catenate SEPARATOR= Expected value for property " ${property} " was " ${value_expected} " but found " ${value_found} " 
    Should Be Equal As Strings ${value_found} ${value_expected} ${error_message} values=false 

*** Test Cases *** 
Example REST JSON 
    Create session ${SERVICE_NAME} ${SERVICE_ROOT} 
    ${headers}= Create Dictionary Content-Type=application/json Accept=application/json 
    ${result}= Get Request ${SERVICE_NAME} ${SERVICE_ROOT} headers=${headers} 
    Should Be Equal ${result.status_code} ${200} 
    Log ${result.content} 
    ${json}= To Json ${result.content} 
    ${pp}= To Json ${result.content} pretty_print=True 
    Log ${pp} 
    json_property_should_equal ${json} ip [Your_IP] 

は、すべての必要なライブラリをインストールすることを忘れないでください

関連する問題