参见英文答案 > Check if two unordered lists are equal 8个
我正在尝试为我的Django应用程序编写测试,我需要多次检查
如果2个列表具有相同的对象(即A中的每个对象也在B中,反之亦然).
我读了assertLists / Sequence / Equal等,但是我看到的是列表
有相同的对象,但顺序不同(A = [a,b,c],B = [b,c,a])然后它返回一个错误,我不希望它是一个错误,因为他们俩有相同的对象.
有没有办法在不循环列表的情况下检查这个?
解决方法:
来自assertCountEqual的Python 3文档:
Test that sequence first contains the same elements as second, regardless of their order. When they don’t, an error message listing the differences between the sequences will be generated.
Duplicate elements are not ignored when comparing first and second. It verifies whether each element has the same count in both sequences. Equivalent to: assertEqual(Counter(list(first)), Counter(list(second))) but works with sequences of unhashable objects as well.
标签:python,unit-testing,django