比较Python中的get请求的预期与实际json响应

时间:2019-02-13 03:11:09

标签: python json

当您访问此网站https://reqres.in/api/users/2

时,您将得到一个小的json响应。

我将响应保存在一个变量中(实际)。我也将响应放入另一个变量(预期)中。两个响应​​都相同。我正在更改值以测试失败的案例。最终目标是比较2并确保它们匹配。

我有2个函数,其中1个比较两个字典的键和值,另一个函数对字典进行排序。下面的代码:

import json
import requests


response = requests.get('https://reqres.in/api/users/2')
#actual_response saves the json as we get it from url above
actual_response= json.loads(response.text)

#expected response is saved after using pretty json that will be used to testing/comparing actual vs expected
expected_response={
    "data": {
        "id": 2,
        "first_name": "Janet",
        "last_name": "Weaver",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
    }
}

# sort the key values before comparing
def dict_sort(dictA,dictB):
    dictA, dictB = json.dumps(dictA, sort_keys=True), json.dumps(dictB, sort_keys=True)
    dictA == dictB

#if there are any failure due to mismatch in key value the function below will show that
def key_diff(dictA,dictB):
    for key,value in dictA.items():
        for keyB,valueB in dictB.items():
            for k,v in value.items():
                for k2,v2 in valueB.items():
                    if(key!= keyB):
                        print('Expected',key,' but got',keyB)
                    if(k!=k2):
                        print('Expected', k, ' but got', k2)
                    if(v!=v2):
                        print('Expected', v, ' but got', v2)
                    else:
                        print()

dict_sort(actual_response,expected_response)

if(actual_response==expected_response):
    print('Passed')
else:
    print('Failed')
    key_diff(actual_response,expected_response)

问题:没有差异时测试就会通过,但是如果有差异,顺序就会变得疯狂。这是一个示例,其中我将数据更改为预期响应内的数据:
预期数据,但数据达标

期望的ID,但姓氏

预计2但得到了Weaver

排序函数应该比使用sort_keys = True更具体吗?通过考虑** args的方式,但我认为在这种情况下这不是一个好选择。

谢谢您的专家评论和时间。

3 个答案:

答案 0 :(得分:0)

在3.7以下的Python版本中不能保证键顺序;当您需要创建一个记住按键顺序的对象时,应该使用collections.OrderedDict

在Python 3.7中,插入顺序被保留,因此您的键将始终匹配。

答案 1 :(得分:0)

我建议使用unittest并避免使用太多嵌套的for循环

from unittest import TestCase

import pandas as pd
import requests


def mocked_server_response():
    expected = {"data": {"id": 2, "first_name": "Janet", "last_name": "Weaver",
                         "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"}}
    data = expected['data']
    df = pd.DataFrame(my_dict['data'], index=[0])
    return [expected, data, df]


在这一点上,mocked_server_response()将为您提供:

Out[27]:
   id first_name last_name                                             avatar
0   2      Janet    Weaver  https://s3.amazonaws.com/uifaces/faces/twitter...

现在,您可以轻松地在课程中进行测试。


class TestServerResponse(TestCase):
    real_response = requests.get('https://reqres.in/api/users/2')

    def setUp(self):
        self.actual_response = real_response

    def response(self):
        self.assertEqual(self.actual_response, mocked_server_response()[0])

    def test_data_in_response(self):
        self.assertEqual(self.actual_response['data'], mocked_server_response()[1])

    def test_dataframe(self):
        self.assertEqual(pd.DataFrame(self.actual_response['data'], index=[0]), mocked_server_response()[2])




答案 2 :(得分:-1)

import requests
import json

# Here I am converting the expected payload in dictionary
expected_payload = json.loads("""[
    {
        "key": "data-center",
        "value": "All",
        "values": [
            "1",
            "2"
        ]
    },
    {
        "key": "router",
        "value": "All",
        "values": [
            "cisco",
            "juniper"
        ]
    },
    {
        "key": "virtual-machine",
        "value": "All",
        "values": [
            "dell",
            "hp",
            "None"
        ]
    },

]""")


def test_get_all_system_attributes():
    url = "http://" + str(ipaddr) + ":" + str(port) + "/Service/" + "system_attribute/"
    payload = {}
    headers = {
        'Content-Type': 'application/json'
    }
    actual_response = requests.request("GET", url, headers=headers, data=payload)
    assert json.loads(actual_response.text) == expected_payload

# json.loads(actual_response.text) will convert the response in dictionary
# using assert I am comparing the actual_response with exepcted_response

if __name__ == '__main__':
    test_get_all_system_attributes()