django unittest抛出异常

时间:2018-04-26 11:07:19

标签: python django

我有问题。当我在Django中创建非常简单的测试时,我有这样的例外:

import unittest 
from django.test import Client

class ModelTest(unittest.TestCase):

    def setUp(self):
        self.client = Client()

    def test_basic(self):
        response = self.client.get('/login/')
        self.assertEqual(response.status_code, 200)

我得到的错误是:

FAILED (errors=1)

Traceback (most recent call last):
  File "C:\Python27\lib\unittest\case.py", line 329, in run
    testMethod()
  File "C:\inetpub\wwwroot\portal\ateris\portal\module\RiskCard\tests\test_models.py", line 11, in test_basic
response = self.client.get('/login/')
  File "C:\Python27\lib\site-packages\django\test\client.py", line 500, in get
**extra)
  File "C:\Python27\lib\site-packages\django\test\client.py", line 303, in get
return self.generic('GET', path, secure=secure, **r)
  File "C:\Python27\lib\site-packages\django\test\client.py", line 358, in generic
data = force_bytes(data, settings.DEFAULT_CHARSET)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__
self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))

ImproperlyConfigured: Requested setting DEFAULT_CHARSET, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

我不知道自己做错了什么。我使用unittest文档。

我正在使用PyCharm

Python ver: 2.7.12

Django ver: 1.8.5

3 个答案:

答案 0 :(得分:0)

将您的代码更改为

from django.test import TestCase
from .models import Client

class UserTestCase(TestCase):
    def setUp(self):
        test_client = Client.objects.get(id=[ClientsID])
    def test_basic(self):
        response = self.client.get('/login/')
        self.assertEqual(response.status_code, 200)  

您错误的部分是您定义self.client = Client(),然后在self.client.get('/login/')包含查询时使用<div id="edit-cv-upload-ajax-wrapper"><div class="control-group form-type-managed-file form-item-cv-upload form-item"> <label for="edit-cv-upload-upload" class="control-label">Upload CV <span class="form-required" title="This field is required.">*</span></label> <div class="controls"> <div id="edit-cv-upload-upload" class="cv-upload form-managed-file"> <input type="file" id="edit-cv-upload-upload" name="files[cv_upload]" size="22" class="form-file error"> <button class="btn form-submit ajax-processed" id="edit-cv-upload-upload-button" name="cv_upload_upload_button" value="Upload" type="submit">Upload</button> <input type="hidden" name="cv_upload[fid]" value="0"> </div> <p class="help-block">Files must be less than <strong>5 MB</strong></p> </div> </div> </div>

答案 1 :(得分:0)

默认情况下django TestCase继承了python单元TestCase并扩展了它的功能,以加速单元测试的过程。默认情况下,它提供客户端。

from django.test import TestCase

class ModelTest(TestCase):

    def test_basic(self):
        response = self.client.get('/login/')
        self.assertEqual(response.status_code, 200)

参考:https://docs.djangoproject.com/en/2.0/topics/testing/advanced/#tests-and-multiple-host-names

答案 2 :(得分:0)

稍作修改,你走了:

from django.test import TestCase

class ModelTest(TestCase):

    def setUp(self):
        self.client = Client()

    def test_basic(self):
        response = self.client.get('/login/')
        self.assertEqual(response.status_code, 200)

一切都会像魅力一样发挥作用。如果你想知道差异。访问here

希望这可以清除你的理解。