我是一个单元测试新手,使用TestCase和mock在Django 1.4中执行以下测试:
from django.test import TestCase
from mock import patch
class BaseRegistrationTestCases(TestCase):
def setUp(self, backend):
# use these common fields for building mock form data
self.form_fields = (
'form_email',
'form_companyname',
'form_companytype',
'form_region',
'form_socialgoals',
)
self.route = '/auth/registration/{backend}/'.format(backend=backend)
@patch('authentication.views.RegistrationView._get_user_by_backend')
@patch('authentication.views.RegistrationView._set_base_context')
def test_invalid_form(self, mock_legacy_uid, mock_base_context, invalid_form_data):
""" Should include invalid fields in context and serve error message """
mock_legacy_uid.return_value = 12348
mock_base_context.return_value = {}
self.response = self.client.post(self.route, invalid_form_data)
class FacebookRegistrationTestCases(BaseRegistrationTestCases):
def setUp(self):
super(FacebookRegistrationTestCases, self).setUp('facebook')
def test_invalid_form(self):
invalid_form_data = {form_field: "" for form_field in self.form_fields}
super(FacebookRegistrationTestCases, self).test_invalid_form(invalid_form_data=invalid_form_data)
for field in self.form_fields:
invalid_key = field + '_invalid'
self.assertEqual(self.response.context[invalid_key], 1)
测试失败,出现以下错误:
======================================================================
ERROR: test_invalid_form (authentication.tests.BaseRegistrationTestCases)
Should include invalid fields in context and serve error message
----------------------------------------------------------------------
TypeError: setUp() takes exactly 2 arguments (1 given)
非常感谢任何帮助!
答案 0 :(得分:1)
BaseRegistrationTestCases
中的setUp()方法需要传递两个参数。这就是为什么你有错误,只有' self'正在通过。
但是,你不能像这样直接将附加参数backend
传递给setUp()!如您所见,它扩展了TestCases,因此您必须覆盖类__init__
以使其与该额外参数一起正常工作。