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

2 python testing django django-testing django-tests

我正在为Django中的自定义管理器编写测试套件,并希望测试大量项目.

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

我需要的是一种在数据库中创建大量 django对象的方法,并将它们保留在整个测试类中,而不是重新创建它们.

我有以下代码:

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.
Run Code Online (Sandbox Code Playgroud)

不幸的是,setUp在每次测试之前调用,但内容在测试期间不会更改,只能读取,并且每次都是相同的.

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

ale*_*cxe 6

你可以使用类方法:setUpClass()

class CustomConceptQuerySetTest(TestCase):
    @classmethod
    def setUpClass(cls):
        ...
Run Code Online (Sandbox Code Playgroud)