比较robot Fram中函数的Json结果

2024-05-13 06:56:21 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试在robot框架中创建测试用例。在

我们有几个restapi返回Json作为结果。为了调用这种情况,我们在rest2.py中使用了以下代码

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

我们将json存储在一个文件输出中。我们编写了robot测试用例来评估json比较,如下所示

^{pr2}$

但是,当我们运行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文件是相同的。但它的失败测试案例表明两者并不相同。这是正确的比较方法吗,还是我们有其他方法可以在robot框架中进行比较。在


Tags: 文件org框架jsonhttpgetrobot测试用例
3条回答

当你这样做时,会有什么反应:

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

我发现你实现你想要的东西的方法有点奇怪,如果我是你,我会使用http库或请求库

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

Get File读取文件内容并返回字符串;同时python函数返回dict对象。所以看起来你在比较dict和字符串-它们根本不能返回equal。在

如果将文件输出转换为dict,则检查很可能通过:

# your other code
${json_file}=   Get file   output
${json_file}=   Evaluate   json.loads("""${json_file}""")    json     # use the json module to transform str->dict

Should Be Equal    ${result}    ${json_file}

# even better - more verbose logging if values differ, or there are missing keys:
Dictionaries Should Be Equal    ${result}    ${json_file}

为什么不使用已经存在的库:

示例代码:

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

记住安装所有需要的库:

^{pr2}$

相关问题 更多 >