Django:防止在每次测试后自动删除模型数据

Django:防止在每次测试后自动删除模型数据,django,django-testing,Django,Django Testing,我已经简化了我的代码以显示这里的效果 class AccountTests(APITestCase): def test_post_account(self): """ Ensure we can create a new account object """ # code that adds one user object and one signup confirmation object ...

我已经简化了我的代码以显示这里的效果

class AccountTests(APITestCase):
    def test_post_account(self):
        """
        Ensure we can create a new account object
        """

        # code that adds one user object and one signup confirmation object
        ...
        ...

        # test we have one user and one confirmation code
        # THIS PASSES OK.   
        self.assertEqual(User.objects.count(), 1)
        self.assertEqual(SignupConfirmationCode.objects.count(), )


    def test_post_confirmation_code(self):
        """
        test sending confirmation code for an account just created
        """
        # THIS FAILS
        self.assertEqual(User.objects.count(), 1)
        self.assertEqual(SignupConfirmationCode.objects.count(), 1)
我知道
test\u post\u账户
首先运行并通过了OK<代码>测试\u确认后\u代码第二次运行,并且由于
用户
注册确认代码
在两种测试方法之间“神奇地”丢失了内容而断言


如何防止数据在第一次测试结束和第二次测试开始之间消失?

您不知道。您设置了测试,以便它们各自创建所需的数据


第一次测试中设置用户和确认的代码应提取到
设置
方法中,该方法在每次测试之前运行。

理解。在某种程度上,这是有道理的。有助于避免顺序混乱并保持测试独立性。