Python assertE除一个键外,等于两个对象

问题描述 投票:1回答:1

我需要测试一个函数,该函数将在rest_framework.test.APITestCase's assertEqual中返回带有django的对象。该对象是这样的:

{
    "first_name": "John",
    "last_name": "Doe",
    "random": some random number
}

如何使用random键以外的合适结果检查返回的对象?

我的意思是,如果传递了这两个对象,则assertEqual(a, result)应该返回True

a = {
        "first_name": "John",
        "last_name": "Doe",
        "random": 12
    }

result = {
        "first_name": "John",
        "last_name": "Doe",
        "random": 24
    }

反正在assertEqual中是否有这种例外情况,或者我必须使用assert

python django testing assert
1个回答
0
投票

您可以创建字典的副本并从此处弹出随机数

a_copy = a.copy().pop("random")
result_copy = result.copy().pop("random")

assertEqual(a_copy, result_copy)
© www.soinside.com 2019 - 2024. All rights reserved.