【问题标题】:Retaining items created during setup of a python test保留在设置 python 测试期间创建的项目
【发布时间】:2015-06-02 05:49:53
【问题描述】:

我正在为 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.

很遗憾,每次测试前都会调用setUp,但测试中内容并没有改变,只是读取,而且每次都是一样的。

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

【问题讨论】:

    标签: python django testing django-testing django-tests


    【解决方案1】:

    你可以使用setUpClass()类方法

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

    【讨论】:

    • 天哪,这很简单。
    猜你喜欢
    • 2014-05-29
    • 1970-01-01
    • 2019-02-23
    • 2010-10-09
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    相关资源
    最近更新 更多