On Fri, 2008-10-10 at 11:51 -0700, Karthik Krishnan wrote: > Hi, > > I am trying to run a unit test for my models. My unit test is as > follows > > import os > import unittest > from myproject import models
Here's the first problem (or maybe two problems). You can't reliably import models from Django applications unless Django already knows what your installed applications are (since relations and the like require knowledge of the applications). So doing the imports before configuring settings is going to lead to some problems that might be hard to diagnose. (The second problem here is why you're importing models from a "project", but I'm guessing you're trying to anonymise the real information.) > DJANGO_PATH = "DJANGO_SETTINGS_MODULE" > os.environ[DJANGO_PATH] = "myproject.settings" > > class ModelsTestCase(unittest.TestCase): > user = None > > def setUp(self): > self.user = models.User.create(first_name='Krishnan', > last_name='Karthik') > > def testUserInstantiation(self): > self.failIfEqual(self.user, None, "Failure to initialize") > > > I always get this error: > > line 57, in _import_settings > raise ImportError("Settings cannot be imported, because > environment variable %s is undefined." % ENVIRONMENT_VARIABLE) > ImportError: Settings cannot be imported, because environment variable > DJANGO_SETTINGS_MODULE is undefined. Unfortunately, you only give the last line of the traceback, without saying what you did to cause it. How are you running the tests? If you're using something like django-admin.py, it will need to know the settings well in advance of getting to executing your test file. If you are trying to run things without using Django's test runner, then you might want to look at using settings.configure() to set up your settings, rather than the environment variable approach. That was written to accommodate manual settings configuration in scripts. On the other hand, just setting the environment variable before using anything at all in Django should work, too (i.e. before import your models that rely on Django). Finally, have a look at the standard manage.py script that Django writes out after running "django-admin.py startproject foo" for another approach to the problem. > This is inspite of the setting the environment settings on top of the > test case. Please let me know if i am doing anything wrong. You are doing something wrong. Django would not raise a fatal exception if you were doing everything right. :-) Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---