如何为admin保存功能编写单元测试

时间:2016-11-24 16:11:47

标签: python django django-unittest

我已经定制了save_model管理员。

class MyModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        # some more question code here
        obj.save()

现在,我想测试MyModelAdmin save_model函数。我尝试发帖如下:

class MyModelAdminSaveTestCase(TestCase):
    def setUp(self):

        # setup code here

    def test_save_model(self):
        '''Test add employee
        '''
        my_obj = {
            'name': 'Tester',
            'address': '12 test Test',
            'city': 'New York',
            'state': 'NY',
        }

        self.client.login(username=self.user, password=self.pwd)
        response = self.client.post(reverse('admin:mymodel_mymodel_add'), my_obj, follow=True)

        self.assertEqual(response.status_code, 200)

        self.assertEqual(MyModel.objects.count(), 1)

然而,测试失败:

self.assertEqual(MyModel.objects.count(), 1)
AssertionError: 0 != 1

0 个答案:

没有答案