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 = "[email protected]" 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() 
4
  • already tried solutions suggested here stackoverflow.com/questions/24011428/… ? Commented Jan 14, 2015 at 8:33
  • @aberna: tried but no help Commented Jan 14, 2015 at 8:57
  • @Alasdair code for test_models.py Commented Jan 14, 2015 at 10:10
  • Do you have full traceback? Commented Jan 14, 2015 at 10:56

1 Answer 1

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() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.