我想知道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)

1号错误消息:
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方法的提示。

最佳答案

尝试这个:

self.assertQuerysetEqual(
    tree_record_qs,
    [repr(r) for r in tree_record_backup]
)

这有点古怪,没有记载;但是,这应该为您工作。

关于django - Django 1.4-assertQuerysetEqual-如何使用方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11610943/

10-09 18:56