【发布时间】:2018-05-20 17:35:42
【问题描述】:
我正在尝试在单个 obj 上运行相同的精确测试,这是一个 models.Model 实例,并且与其他模型有一些关系。我不想在那个实例中持久化更改,所以实际上我想要回滚事务的tearDown 方法的相同效果。
为了说明这一点:
class MyTestCase(django.test.TestCase):
def test():
# main test that calls the same test using all
# different states of `obj` that need to be tested
# different values of data that update the state of `obj`
# with state I simply mean the values of `obj`'s attributes and relationships
data = [state1, state2, state3]
for state in data:
obj = Obj.objects.get(pk=self.pk) # gets that SINGLE object from the test db
# applies the state data to `obj` to change its state
obj.update(state)
# performs the actual test on `obj` with this particular state
self._test_obj(obj)
def _test_obj(self, obj):
self.assertEqual(len(obj.vals), 10)
self.assertLess(obj.threshold, 99)
# more assert statements...
这个设计有两个问题:
-
李>obj上的更改会保留在测试数据库中,因此在下一次迭代中,数据将被污染。我想回滚这些更改并获得obj的新鲜 实例,就像刚刚调用了test方法一样,我们直接从固定装置获取数据。 如果断言语句失败,我将能够看到它是哪一个,但由于
for,我将无法确定 what 案例(状态)失败环形。我可以在test方法中try-except_test_obj_调用,但我无法判断什么断言失败。
django.test 是否提供任何工具来针对同一模型的不同状态运行相同的测试?如果没有,在解决上述两个问题的同时,我该如何做我想做的事情?
【问题讨论】:
标签: python django python-2.7 testing django-1.8