assertEqual 的局限性

Limitations of assertEqual

docs for assertEqual in Python unittest

assertEqual(first, second, msg=None)

Test that first and second are equal. If the values do not compare equal, the test will fail

如果我的输入 firstsecond 是深层嵌套对象(例如 dict of list of dict 和 list 等),对于不能与上述断言进行比较的内容是否有任何限制陈述?到目前为止,我知道如果在任何深度都有一个列表,它的顺序必须在两侧匹配(因为这是我通常比较列表的方式)。

文档中没有具体提及嵌套对象,我找不到明确的答案。

assertEqual 调用适当的 type equality function (if available). e.g. assertEqual on lists actually calls assertListEqual. If no type equality function is specified, assertEqual simply uses the == operator 来确定相等性。

请注意,您可以根据需要创建和注册自己的类型相等函数。

如果您想查看实际实现,assertListEqual 只需将 assertSequenceEqual 委托给 ultimately uses != to compare items。如果子项是嵌套的,则会进行比较,但是 python 会比较这些项。例如,列表被认为是相等的如果:

Sequence types also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length. (For full details see Comparisons in the language reference.)

请参阅 python sequences 上的文档。