Django:运行单元测试时出现错误配置错误

时间:2014-11-18 22:57:00

标签: python django unit-testing

我正在尝试在Django中编写我的第一个单元测试(运行1.5)。这是一个简单的测试,看看主页是否返回200状态。代码位于我的某个应用中的文件tests.py中。该文件如下所示:

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)

当我运行manage.py test app_name.HomePageTestCase命令时,出现以下错误:

======================================================================
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"

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

我有什么想法可以解决这个问题?提前谢谢。

编辑1:

来自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',
)

来自settings.py文件的相关应用:

INSTALLED_APPS = (
...

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

...
)

编辑2:

'middleware.CustomMiddleware',评论MIDDLEWARE_CLASSES后,我收到了一条新错误:

======================================================================
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

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

看起来它可能是一个python路径问题或什么?我不是专家,这是肯定的。

编辑3:

文件结构大致如下:

.
├── 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

1 个答案:

答案 0 :(得分:0)

看起来项目的路径不在系统路径中。所以我把它添加到测试文件的顶部:

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

感觉有点hacky但是......它有效!我确定在它的路径上存在文件结构问题。