python - 对同一对象的不同状态进行相同的测试,而无需持久更改

标签 python django python-2.7 testing django-1.8

我正在尝试在单个 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 上的更改会保留在测试数据库中,因此在下一次迭代中数据将被污染。我想回滚这些更改并获得 objfresh 实例,就好像刚刚调用了 test 方法并且我们正在直接获取数据一样来自固定装置。

  • 如果断言语句失败,我将能够看到它是哪一个,但我将无法确定什么案例(状态)因而失败for 循环。我可以在 test 方法中 try-except _test_obj_ 调用,但我无法分辨什么 断言失败。

django.test 是否提供任何工具来对同一模型的不同状态运行相同的测试?如果没有,我如何在解决上述两点的同时做我想做的事情?

最佳答案

  1. 使用完对象后简单地回滚。

  2. 您可以使用新的 subTest在 python 3.4+ 中


您的代码应如下所示:

class TestProductApp(TestCase):
    def setUp(self):
        self.product1 = ...

    def test_multistate(self):
        state1 = dict(name='p1')
        state2 = dict(name='p2')

        data = [state1, state2]

        for i, state in enumerate(data):
            with self.subTest(i=i):
                try:
                    with transaction.atomic():
                        product = Product.objects.get(id=self.product1.id)
                        product.name = state['name']
                        product.save()
                        self.assertEqual(len(product.name), 2)
                        raise DatabaseError #forces a rollback
                except DatabaseError:
                    pass
                print(Product.objects.get(id=self.product1.id)) #prints data created in setUp/fixture

关于python - 对同一对象的不同状态进行相同的测试,而无需持久更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47681046/

相关文章:

python - Django Defect-dojo:aws-prowler上传失败

Django .get() 在没有显式 '__exact' 查找的情况下引发了DoesNotExist 错误

python - __eq__ 应该比较两种不同类型的对象吗?

python - 使用字典计算字符串中的第一个字母 - Python

python - 解析和聚合 Google Analytics 浏览器版本 CSV 数据

python Pandas : trying to vectorize a function using date_range

django - 如何将独立的 Bokeh 图嵌入到 Django 模板中

Python正则表达式查找两个子字符串之间的所有字符串

python - 如何从 Django 中的另一个模型中选择所有外键对象?

python - 用asyncore实现gethostbyaddr()