Python:在pytest中比较两个JSON对象

8

我有一个API,它返回以下JSON响应

{
    "message": "Staff name and password pair not match",
    "errors": {
        "resource": "Login",
        "field": "staff_authentication",
        "code": "invalid",
        "stack_trace": null
    }
}

使用pytest,我想要构建JSON对象的副本,并确保其完全相同。
import pytest
import json
from collections import namedtuple
from flask import url_for
from myapp import create_app

@pytest.mark.usefixtures('client_class')
class TestAuth:

    def test_login(self, client):
        assert client.get(url_for('stafflogin')).status_code == 405
        res = self._login(client, 'no_such_user', '123456')
        assert res.status_code == 422
        response_object = self._json2obj(res.data)
        assert response_object.message == 'Staff name and password pair not match'
        invalid_password_json = dict(message="Staff name and password pair not match",
                                    errors=dict(
                                        resource="Login",
                                        code="invalid",
                                        field="staff_authentication",
                                        stack_trace=None,)
                                    )
        assert self._ordered(response_object) == self._ordered(invalid_password_json)

    def _login(self, client, staff_name, staff_password):
        return client.post('/login',
            data=json.dumps(dict(staff_name=staff_name, staff_password=staff_password)),
            content_type='application/json',
            follow_redirects=True)

    def _json_object_hook(self, d): return namedtuple('X', d.keys())(*d.values())
    def _json2obj(self, data): return json.loads(data, object_hook=self._json_object_hook)

    def _ordered(self, obj):
        if isinstance(obj, dict):
            return sorted((k, self._ordered(v)) for k, v in obj.items())
        if isinstance(obj, list):
            return sorted(self._ordered(x) for x in obj)
        else:
            return obj

pytest 显示这两个对象不相等。

>       assert self._ordered(response_object) == self._ordered(invalid_password_json)
E       AssertionError: assert X(message='St...k_trace=None)) == [('errors', [(...r not match')]
E         At index 0 diff: 'Staff name and password pair not match' != ('errors', [('code', 'invalid'), ('field', 'staff_authentication'), ('resource', 'Login'), ('stack_trace', None)])
E         Full diff:
E         - X(message='Staff name and password pair not match', errors=X(resource='Login', field='staff_authentication', code='invalid', stack_trace=None))
E         + [('errors',
E         +   [('code', 'invalid'),
E         +    ('field', 'staff_authentication'),
E         +    ('resource', 'Login'),
E         +    ('stack_trace', None)]),
E         +  ('message', 'Staff name and password pair not match')]

tests/test_app.py:31: AssertionError
=========================== 1 failed in 0.22 seconds ===========================

如何使新创建的JSON对象与响应相同?
2个回答

8

我使用json.loads()将 JSON 响应转换为字典,然后进行比较,而不是将其转换为对象

def test_login(self, client):
        res = return client.post('/login',
            data=json.dumps(dict(staff_name='no_such_user', staff_password='password')),
            content_type='application/json',
            follow_redirects=True)
        assert res.status_code == 422
        invalid_password_json = dict(message="Staff name and password pair not match",
                                    errors=dict(
                                        resource="Login",
                                        code="invalid",
                                        field="staff_authentication",
                                        stack_trace=None,),
                                    )
        assert json.loads(res.data) == invalid_password_json

这样,我就不必担心JSON响应中的空格差异以及JSON结构的顺序。只需让Python的字典比较函数检查相等性即可。


1
如果您确实需要两个字典之间的文字值相等性,那么比较它们的JSON序列化结果会更简单,否则您需要对字典及其值进行递归比较。
注意:由于Python中的字典是无序集合,因此您需要将sort_keys = True传递给json.dumps,有关更多详细信息,请参见此问题 this question

1
谢谢你的回答。sort_keys=True 是必要的吗?即使我打乱了键,比较仍然是正确的。 - Hanxue
1
如果您使用的是Python3,那么很可能是因为当前实现的字典是有序的,但据我所知,这并不是预期的,也没有包含在规范中,因此它可以工作,但从长远来看我不会依赖它。在Python2中,我认为这通常不应该是相同的,但对于固定键列表的短字典,它可能可以在不排序键的情况下工作。 - E P
1
Python3 已经承诺字典将来会有序,所以你可以依赖它。 - Marc
在现代Python中,字典不仅是有序的,而且不同顺序的字典可以相互比较相等。 - Joooeey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接