Django注册测试脚本没有运行

时间:2017-04-08 23:09:47

标签: python django unit-testing

我试图在我的Django网站上为自定义注册表单单元测试,但在运行python manage.py test时出错。我试图运行以下测试:

def test_registration_valid_view(self):
    '''

    '''
    pass
    user_count = User.objects.count()
    form_data = {'username': 'test@example.com',
                 'first_name': 'Test',
                 'last_name': 'Person',
                 'password1': 'admin123',
                 'password2': 'admin123'}
    response = self.client.post("/sign-up/",
                                form_data)

    self.assertEqual(response.status_code, 200)
    self.assertEqual(User.objects.count(), user_count+1)

但我收到以下错误:

======================================================================
ERROR: test_registration_valid_view (accounts.tests.RegistrationTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/haldunanil/Desktop/cashonly/accounts/tests.py", line 59, in test_registration_valid_view
    form_data)
  File "/Users/haldunanil/anaconda/envs/ebenv/lib/python3.6/site-packages/django/test/client.py", line 541, in post
    secure=secure, **extra)
  File "/Users/haldunanil/anaconda/envs/ebenv/lib/python3.6/site-packages/django/test/client.py", line 343, in post
    secure=secure, **extra)
  File "/Users/haldunanil/anaconda/envs/ebenv/lib/python3.6/site-packages/django/test/client.py", line 409, in generic
    return self.request(**r)
  File "/Users/haldunanil/anaconda/envs/ebenv/lib/python3.6/site-packages/django/test/client.py", line 494, in request
    six.reraise(*exc_info)
  File "/Users/haldunanil/anaconda/envs/ebenv/lib/python3.6/site-packages/django/utils/six.py", line 686, in reraise
    raise value
  File "/Users/haldunanil/anaconda/envs/ebenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = get_response(request)
  File "/Users/haldunanil/anaconda/envs/ebenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/haldunanil/anaconda/envs/ebenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/haldunanil/Desktop/cashonly/accounts/decorators.py", line 27, in wrap
    return function(request, *args, **kwargs)
  File "/Users/haldunanil/Desktop/cashonly/accounts/views.py", line 16, in registration
    form.save()
  File "/Users/haldunanil/Desktop/cashonly/accounts/forms.py", line 32, in save
    group = Group.objects.get(name='Consumers')
  File "/Users/haldunanil/anaconda/envs/ebenv/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/haldunanil/anaconda/envs/ebenv/lib/python3.6/site-packages/django/db/models/query.py", line 385, in get
    self.model._meta.object_name
django.contrib.auth.models.DoesNotExist: Group matching query does not exist.

----------------------------------------------------------------------

不确定这里的问题是什么。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的错误与此测试用例无关。 Django在这方面失败了:

Group.objects.get(name='Consumers')

你没有任何名字和消费者群体。请记住,Django为单元测试设置了一个新的数据库,因此您必须在运行测试用例之前填充测试数据库。