Django测试用例ValueError:必须设置给定的用户名

时间:2018-12-21 11:18:38

标签: django django-tests

我正在测试Django应用程序的用户注册功能,以确定发布的数据是否正确以及发布请求是否成功执行。

在views.py中,CustomerSignUpView类

class CustomerSignUpView(View):
  def post(self, request):
    name_r = request.POST.get('customer_username')
    password_r = request.POST.get('customer_password')
    email_r = request.POST.get('customer_email')

    contact_number_r = request.POST.get('customer_contact_number')
    profile_picture_r = request.POST.get('customer_profile_picture')

    if checkemail(email_r):
        # receiving an error here in TEST CASE not in actual program execution
        c = User(username=name_r, password=password_r, email=email_r)
        c.save()

        p = Profile(user=c, phone_number=contact_number_r, profile_picture=profile_picture_r)
        p.save()

        return render(request, 'catalog/customer_login.html')
    else:
        return render(request, 'catalog/customer_signup.html')

  def get(self, request):
    return render(request, 'catalog/customer_signup.html')

这是创建用户帐户的测试用例:

  class CustomerSignUpViewTest(TestCase):
"""
    Test case for User Sign in
"""
    def test_registration_view_post_success(self):
    """
        A ``POST`` to the ``customer_signup`` view with valid data properly
        creates a new user and issues a redirect.
    """
       data = {
        'username': 'testuser1',
        'password': '1X<ISRUkw+tuK',
        'email': 'foobar@test.com',
        'phone_number': '9876543210',
       }
       response = self.client.post(reverse('customer_signup'), data, follow=True)
       self.assertEqual(response.status_code, 200)
       self.assertTrue(response.url.startswith('/catalog/customer_login/'))

测试遇到以下错误:ValueError('必须设置给定的用户名')

Error
Traceback (most recent call last):
File "/Users/sndtcsi/PycharmProjects/Library/catalog/tests.py", line 54, in test_registration_view_post_success
response = self.client.post(reverse('customer_signup'), data, follow=True)
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/test/client.py", line 535, in post
response = super().post(path, data=data, content_type=content_type, secure=secure, **extra)
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/test/client.py", line 349, in post
secure=secure, **extra)
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/test/client.py", line 414, in generic
return self.request(**r)
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/test/client.py", line 495, in request
raise exc_value
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/Users/sndtcsi/PycharmProjects/Library/catalog/views.py", line 107, in post
c = User.objects.create_user(username=name_r, password=password_r, email=email_r)
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/contrib/auth/models.py", line 150, in create_user
return self._create_user(username, email, password, **extra_fields)
File "/Users/sndtcsi/PycharmProjects/Library/venv/lib/python3.7/site-packages/django/contrib/auth/models.py", line 139, in _create_user
raise ValueError('The given username must be set')

虽然我的注册功能运行良好,但测试执行表明未设置用户名。我不明白为什么会这样。

1 个答案:

答案 0 :(得分:1)

您在TestCase中使用了错误的密钥。您试图在视图中获取表单中的customer_username,但实际上是在TestCase的 data 中发布username键。为此,您应该使这些行使用相同的键:

name_r = request.POST['customer_username']  
and
'username': 'testuser1',

此代码中的所有其他表单都存在相同的问题。