【问题标题】:comparing Json result from a function in robot Framework比较机器人框架中函数的 Json 结果
【发布时间】:2016-08-23 07:32:38
【问题描述】:

试图在机器人框架中创建一个测试用例。

我们有几个 rest 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()

我们将 json 存储在文件输出中。我们编写了机器人测试用例来评估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}

但是当我们运行 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 文件是一样的。但是它的失败测试用例仍然说两者都不相同。这是比较的正确方法,还是我们在机器人框架中有任何其他方法可以做到这一点。

【问题讨论】:

    标签: python robotframework


    【解决方案1】:

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

    示例代码:

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

    记得安装所有需要的库:

    pip install robotframework-requests
    pip install requests
    

    【讨论】:

      【解决方案2】:

      你这样做的反应是什么:

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

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

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

      【讨论】:

        【解决方案3】:

        Get File读取文件内容并返回一个字符串;同时python函数返回一个dict对象。所以看起来你正在比较一个 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}
        

        【讨论】:

        • OP 问题的答案!
        【解决方案4】:

        工作示例

        将其放入 .robot 文件中

        *** Settings ***
        Library    jsonlib.py
        
        *** Variables ***
        ${response_json}    {"key1": 33, "key2": "string", "key3": [1,2,3,"sring"], "name": "hans"}
        ${expected_json}    {"key1": 77, "key2": "string", "key3": [1,2,3,"sring"], "name": "OTTO", "age": 33}
        
        *** Test Cases ***
        response meets expectation
            &{json1}=    Evaluate    json.loads('''${response_json}''')    json
            &{json2}=    Evaluate    json.loads('''${expected_json}''')    json
            Log Dictionary    ${json1}
            Log Dictionary    ${json2}
            # ${type}=    evaluate    type(${json1}).__name__
            # ${type}=    evaluate    type(&{json2})
            response_meets_expectation  ${json1}  ${json2}
        
        compare json payloads
            &{json1}=    Evaluate    json.loads('''${response_json}''')    json
            &{json2}=    Evaluate    json.loads('''${expected_json}''')    json
            compare_json_payloads  ${json1}  ${json2}
        
        json payloads should match
            &{json1}=    Evaluate    json.loads('''${response_json}''')    json
            &{json2}=    Evaluate    json.loads('''${expected_json}''')    json
            json_paylodas_should_match  ${json1}  ${json2}
        

        将它放在 jsonlib.py 中,与 .robot 文件位于同一文件夹中

        # -*- coding: utf-8 -*-
        
        import json
        import jsondiff
        from deepdiff import DeepDiff
        # from deepdiff import DeepSearch, grep
        from pprint import pprint
        from robot.api import logger
        
        
        class JsonCompareError(Exception):
            pass
        
        def response_meets_expectation(response, expectation):
            union = jsondiff.patch(response, expectation)
            difference = jsondiff.diff(response, union)
            if difference:
                    raise JsonCompareError("Diff found: {}".format(difference))
            else:
                return True
        
        def compare_json_payloads(response_json, expected_json):
            logger.debug("Compare JSON payloads")
            diff = DeepDiff(response_json, expected_json, ignore_order=True, verbose_level=2)
            logger.debug("Values changed: {}".format(diff['values_changed']))
            return diff.to_dict()
        
        def json_paylodas_should_match(response_json, expected_json):
            diff = DeepDiff(response_json, expected_json, verbose_level=2)
            if diff != {}:
                raise JsonCompareError("Payloads do not match! Differences: {}".format(diff))
            else:
                return True
        

        【讨论】:

          【解决方案5】:

          使用https://github.com/Accruent/robotframework-zoomba 会是这样的:

          Library         Zoomba.APILibrary
          
          *** Test Cases ***
          Example Test
             ${headers}=  Create Dictionary  Content-Type=application/json  Accept=application/json
             ${actual_response}=   Call Get Request   ${headers}    http://httpbin.org/get
             Validate Response Contains Expected Response     ${actual_response}    ${expected_response}
          

          这将逐个验证结果,任何失败的结果都会如下所示:

          Key(s) Did Not Match:
          ------------------
          Key: pear
          Expected: fish
          Actual: bird
          ------------------
          Full List Breakdown:
          Expected: [{'apple': 'cat', 'banana': 'dog', 'pear': 'fish'}, {'apple': 'cat', 'banana': 'mice', 'pear': 'bird'}, {'apple': 'dog', 'banana': 'mice', 'pear': 'cat'}]
          Actual: [{'apple': 'cat', 'banana': 'dog', 'pear': 'bird'}]
          
          Please see differing value(s)"
          

          这里的文档:https://accruent.github.io/robotframework-zoomba/docs/APILibraryDocumentation.html

          【讨论】:

            猜你喜欢
            • 2020-07-12
            • 1970-01-01
            • 2021-02-04
            • 1970-01-01
            • 1970-01-01
            • 2019-10-12
            • 2018-05-19
            • 2017-05-06
            • 2018-03-18
            相关资源
            最近更新 更多