django中test.py中的许多Testcase类都无法正常运行

时间:2015-10-16 16:46:22

标签: python django django-unittest

我希望在我的django项目中的test.py中有不同的Testcase类。一个用于模型测试(crud)一个用于视图测试等。我有以下类

class EntryModelTest(TestCase):
    LOG = logging.getLogger(__name__)

    @classmethod
    def setup_database(cls):
       #database initialization


    @classmethod
    def setUpClass(cls):
        super(EntryModelTest, cls).setUpClass()
        cls.setup_database()
        cls.myModel = MyModel.objects.get(pk=1)
        #some queries (additions) that use things that were added in setup_database()

和第二个测试用例

class MyCalViewsTest(TestCase):
    LOG = logging.getLogger(__name__)

    @classmethod
    def setup_database(cls):
        #exactly the same code as above testcase(I know I could use Inheretiance. Will refactor)

    @classmethod
    def setUpClass(cls):
        #The same as above testcase
        cls.myModel = MyModel.objects.get(pk=1) #this gives the following error: DoesNotExist: PracticeData matching query does not exist.

MyModel实例是在setup_database方法中创建的。我不太清楚为什么。如果在执行每个测试用例类之后销毁测试数据库,则setup_database方法应该重新创建具有pk = 1的MyModel实例,因此我本来就存在。如果它没有销毁测试数据库,那么pk = 1的MyModel实例已经存在,所以我也不应该有任何问题。任何帮助,将不胜感激。

0 个答案:

没有答案