保留 python 测试设置期间创建的项目

Retaining items created during setup of a python test

我正在为 Django 中的自定义管理器编写测试套件,并且想要测试大量项目。

本质上,由于组合爆炸,它需要创建数千个项目。

我需要的是一种在数据库中创建 lot django 对象并在整个测试中保留它们 class 而不是重新创建它们的方法。

我有以下代码:

class CustomConceptQuerySetTest(TestCase):
    def setUp(self):
        pass #make lots and lots of items.

    def test_is_public(self):
        pass # check if returned items in the object.public() queryset are actually "public"
    def test_is_editable(self):
        pass # check if returned items in the object.viewable() queryset are actually "viewable" only to certain users.

遗憾的是,每次测试前都会调用setUp,但测试时内容不变,只读,每次都一样

在 Django 中有没有办法保留数据库,或者防止在测试中回滚或破坏 Class?

可以使用setUpClass()class方法:

class CustomConceptQuerySetTest(TestCase):
    @classmethod
    def setUpClass(cls):
        ...