我想知道 TestCase.assertQuerysetEqual 方法是如何工作的。我以不同的方式进行了尝试,每种方式都将我引导到另一个错误消息。
#create a backup of all records in the tree
tree_record_backup = list(Tree.objects.all())
#do some updates on another table, which should not affect the tree table if everything goes wrong
#check if list of tree records did not changed
tree_record_qs = Tree.objects.all()
#Number1:
self.assertQuerysetEqual(tree_record_qs,[repr(tree_record_backup)])
#Number2:
self.assertQuerysetEqual(tree_record_qs,tree_record_backup)
Number1 的错误消息:
First list contains 21 additional elements.
First extra element 1:
node.pk: 2 - node: node2 - pk: 2 - level: 0 - ancestor: 2
编号 2 的错误消息:
AssertionError: Lists differ: ['<Tree: node.pk: 1 - node: ro... != [<Tree: node.pk: 1 - node: roo...
First differing element 0:
<Tree: node.pk: 1 - node: root - pk: 1 - level: 0 - ancestor: 1>
node.pk: 1 - node: root - pk: 1 - level: 0 - ancestor: 1
感谢您提示如何正确使用 assertQuerysetEqual 方法。