Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django注册测试脚本未运行_Python_Django_Unit Testing - Fatal编程技术网

Python Django注册测试脚本未运行

Python Django注册测试脚本未运行,python,django,unit-testing,Python,Django,Unit Testing,我试图在Django网站上为自定义注册表编写单元测试,但在运行python manage.py测试时出错。我正在尝试运行以下测试: def test_registration_valid_view(self): ''' ''' pass user_count = User.objects.count() form_data = {'username': 'test@example.com', 'first_name':

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

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.

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

我不知道这里有什么问题。有什么想法吗?

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

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


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

谢谢,完全没有做到这一点!