尝试运行单个django-nose测试时,admin.autodiscover()下的ImportError

时间:2015-05-26 16:45:27

标签: python unit-testing nose django-nose

我正在尝试使用django-nose运行单一测试:

./manage.py test lead.tests:LeadStatTests.basic_stat_test

发生以下ImportError:

======================================================================
ERROR: Failure: ImportError (cannot import name LeadSource)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/nose/loader.py", line 420, in loadTestsFromName
    addr.filename, addr.module)
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/Users/kolen/items/someproject/lead/tests.py", line 16, in <module>
    from lead.models import Lead, Action, LeadSource, LeadType, events_by_month, EventSummary
  File "/Users/kolen/items/someproject/lead/models.py", line 38, in <module>
    User = get_user_model()
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 127, in get_user_model
    user_model = get_model(app_label, model_name)
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/db/models/loading.py", line 271, in get_model
    self._populate()
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in _populate
    self.load_app(app_name)
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/db/models/loading.py", line 99, in load_app
    models = import_module('%s.models' % app_name)
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/debug_toolbar/models.py", line 63, in <module>
    patch_root_urlconf()
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/debug_toolbar/models.py", line 51, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 507, in reverse
    app_list = resolver.app_dict[ns]
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 329, in app_dict
    self._populate()
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 267, in _populate
    for pattern in reversed(self.url_patterns):
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 365, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 360, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/Users/kolen/items/someproject/system/urls.py", line 8, in <module>
    admin.autodiscover()
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 29, in autodiscover
    import_module('%s.admin' % app)
  File "/Users/kolen/.virtualenvs/someenv/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/Users/kolen/items/someproject/lead/admin.py", line 5, in <module>
    from lead.models import LeadSource, LeadType, Lead, ........, \
ImportError: cannot import name LeadSource

使用./manage.py test运行所有测试。仅在尝试运行单个测试或测试类时才会发生此异常。但是,运行所有测试需要花费太多时间,所以我想要一种方法来运行单个测试。

应用程序和django管理员也可以。项目的根目录中没有__init__.py

1 个答案:

答案 0 :(得分:0)

突然发现问题是由django-debug-toolbar引起的。从项目中删除它解决了这个问题。

django-debug-toolbar也导致possibly duplicate of this question with similar problem