2

I'm trying to run some testes for django (1.7) project.

have created a test_models.py directory /tests/ under project directory.

On running test

>> python tests/test_models.py -v

Error:

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

Although following django stanards commands works fine

>> python manage.py runserver
>> python manage.py shell

test_models.py

import unittest
from django.contrib.auth.models import User
from accounts.models import school

class TestAccounts(unittest.TestCase):

    def setUp(self):
        admin_user = User.objects.get_or_create(
            username="testuser", password=111)
        self.admin = admin_user[0]

        self.school_name = "merucabs"
        self.email = "info@merucabs.com"

    def test_school(self):
        print "Testing school ..."
        school = School(name=self.school_name)
        school.created_by = self.admin
        school.updated_by = self.admin
        school.save()
        self.school = school
        self.assertEqual(self.school.name, self.school_name)



def suite():
    suite = unittest.TestSuite()
    suite.addTests(unittest.makeSuite(TestAccounts))

if __name__ == '__main__':
    unittest.main()
navyad
  • 3,752
  • 7
  • 47
  • 88

1 Answers1

6

You can run Django tests with the test command.

./manage.py test tests/test_models

If you want to run your tests as a stand alone script (i.e. python tests/test_models.py), then you have to set the DJANGO_SETTINGS_MODULE environment variable. As of Django 1.7, you must call django.setup() as well.

import django
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
django.setup()
Alasdair
  • 298,606
  • 55
  • 578
  • 516