简体   繁体   中英

Django: ImproperlyConfigured error when running unit test

I'm trying to write my first unit test in Django (running 1.5). It's a simple test to see if the home page returns a 200 Status. The code is in a file tests.py in one of my apps. The file looks like this:

from django.test import TestCase
from django.test.client import Client

class HomePageTestCase(TestCase):
    def setUp(self):
        self.c = Client()

    def test_anonymous_home_page(self):
        """Test the home page when visited anonymously"""

        response = self.c.get('/')
        self.assertEqual(response.status_code, 200)

When I run the manage.py test app_name.HomePageTestCase command, I get the following error:

======================================================================
ERROR: test_anonymous_home_page (project.my_app.tests.HomePageTestCase)
Test the home page when visited anonymously
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../project/my_app/tests.py", line 12, in test_anonymous_home_page
    response = self.c.get('/')
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/test/client.py", line 453, in get
    response = super(Client, self).get(path, data=data, **extra)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/test/client.py", line 279, in get
    return self.request(**r)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/test/client.py", line 406, in request
    response = self.handler(environ)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/test/client.py", line 102, in __call__
    self.load_middleware()
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/core/handlers/base.py", line 53, in load_middleware
    raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware middleware: "No module named middleware"

----------------------------------------------------------------------

Any ideas how I might fix this? Thanks in advance.

Edit 1:

Middleware from settings.py :

MIDDLEWARE_CLASSES = (
    'johnny.middleware.LocalStoreClearMiddleware',
    'johnny.middleware.QueryCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'middleware.CustomMiddleware',
)

Relavent apps from settings.py file:

INSTALLED_APPS = (
...

    'django.contrib.admin',
    'django.contrib.admindocs',

...
)

Edit 2:

After commenting 'middleware.CustomMiddleware', out from MIDDLEWARE_CLASSES I get a new error:

======================================================================
ERROR: test_anonymous_home_page (project.my_app.tests.HomePageTestCase)
Test the home page when visited anonymously
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../project/my_app/tests.py", line 11, in test_anonymous_home_page
    response = c.get('/')
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/test/client.py", line 453, in get
    response = super(Client, self).get(path, data=data, **extra)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/test/client.py", line 279, in get
    return self.request(**r)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/test/client.py", line 424, in request
    six.reraise(*exc_info)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/core/handlers/base.py", line 103, in get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/core/urlresolvers.py", line 321, in resolve
    sub_match = pattern.resolve(new_path)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/core/urlresolvers.py", line 223, in resolve
    return ResolverMatch(self.callback, args, kwargs, self.name)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/core/urlresolvers.py", line 230, in callback
    self._callback = get_callable(self._callback_str)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/utils/functional.py", line 31, in wrapper
    result = func(*args)
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/core/urlresolvers.py", line 101, in get_callable
    not module_has_submodule(import_module(parentmod), submod)):
  File "/Applications/djangostack-1.5.1-0/apps/django/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named my_app

----------------------------------------------------------------------

It looks like it might be a python path problem or something? I'm no expert in it, that's for sure.

Edit 3:

File structure looks roughly like this:

.
├── project
│   ├── email
│   │   └── management
│   │       └── commands
│   ├── app_name
│   │   ├── api
│   │   ├── migrations
│   │   └── test.py
│   ├── library
│   │   └── templatetags
│   ├── sitemaps
│   │   ├── exports
│   │   └── management
│   │       └── commands
│   ├── app_name_2
│   │   ├── api
│   │   ├── management
│   │   │   └── commands
│   │   └── migrations
│   ├── templates
│   └── app_name_3
│       ├── api
│       └── migrations
└── manage.py

Looks like the path to the project wasn't in the system paths. So I added this to the top of the test file:

import sys
sys.path.append('/the/way/to/the/project')

Feels a little hacky but... it works! I'm sure there's a file structure issue at the route of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM