2

I have a problem. When I create very simple test in Django I have exceptions like this:

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)

The error I get is:

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.

I don't know what I am doing wrong. I use the unittest documentation.

I am using PyCharm

Python ver: 2.7.12

Django ver: 1.8.5

Ralf
  • 16,086
  • 4
  • 44
  • 68
swozny13
  • 71
  • 1
  • 3

3 Answers3

0

Change your code as

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)  

Your wrong part is that you define self.client = Client() and then you use self.client.get('/login/') while it contains a query.

  • What is a difference between import unittest a django.test import TestCase? The client is not object in my models. I use this to simulate GET and POST requests on a URL and observe the response. – swozny13 Apr 26 '18 at 11:25
0

By default django TestCase inherits the python unit TestCase and extends it's functionality to speedup the process of unit Testing. By default it provides client.

from django.test import TestCase

class ModelTest(TestCase):

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

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

anjaneyulubatta505
  • 10,713
  • 1
  • 52
  • 62
0

Just a little modification, here you go:

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)

Everything will work like a charm. In case you are curious to know the differences. Visit here

Hope that might clear your understanding.

Abhimanyu
  • 725
  • 1
  • 6
  • 20