【问题标题】:Running django tutorial tests fail - No module named polls.tests运行 django 教程测试失败 - 没有名为 polls.tests 的模块
【发布时间】:2014-01-30 22:26:36
【问题描述】:

我正在使用 django 1.6 教程,但我无法运行测试。 我的项目(名称 mydjango)和应用程序结构(名称是 polls)在 virtualenv 中如下所示。 (.nja 文件只是由 ninja-ide 我正在使用的 ide 创建的)

.
├── __init__.py
├── manage.py
├── mydjango
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── mydjango.nja
│   ├── settings.py
│   ├── settings.pyc
│   ├── templates
│   │   └── admin
│   │       └── base_site.html
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── polls
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   ├── __init__.py
│   │   └── polls
│   │       ├── detail.html
│   │       ├── index.html
│   │       ├── __init__.py
│   │       └── results.html
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
└── polls.nja

我按照教程了解了 django 的工作原理,但我陷入了测试部分。 正如教程建议的那样,我在 app 文件夹中创建了一个名为 tests.py 的文件,非常简单的文件是:

# -*- coding: utf-8 -*-
from django.test import TestCase
import datetime
from django.utils import timezone
from polls.models import Question

# Create your tests here.l  
class QuestionMethodTests(TestCase):

    def test_was_published_recently_with_future_poll(self):
        """
        was_published_recently dovrebbe ritornare falso se si mette una data nel futuro
        """
        future_question = Question(pub_date=timezone.now() + datetime.timedelta(hours=50))
        self.assertEqual(future_question.was_published_recently(), False)

然后我将 unittest2 安装到 virtualenv 中

$pip install unittest2

然后运行

$python manage.py test polls
Creating test database for alias 'default'...
E
======================================================================
ERROR: mydjango.polls.tests (unittest2.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: mydjango.polls.tests
Traceback (most recent call last):
  File "/home/sergio/.virtualenvs/django4/local/lib/python2.7/site-packages/unittest2/loader.py", line 260, in _find_tests
    module = self._get_module_from_name(name)
  File "/home/sergio/.virtualenvs/django4/local/lib/python2.7/site-packages/unittest2/loader.py", line 238, in _get_module_from_name
    __import__(name)
ImportError: No module named polls.tests


----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
Destroying test database for alias 'default'...

没有办法让测试工作,如果不通过应用程序名称,它也会返回相同的错误:

$ python manage.py test
Creating test database for alias 'default'...
E
======================================================================
ERROR: mydjango.polls.tests (unittest2.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: mydjango.polls.tests
Traceback (most recent call last):
  File "/home/sergio/.virtualenvs/django4/local/lib/python2.7/site-packages/unittest2/loader.py", line 260, in _find_tests
    module = self._get_module_from_name(name)
  File "/home/sergio/.virtualenvs/django4/local/lib/python2.7/site-packages/unittest2/loader.py", line 238, in _get_module_from_name
    __import__(name)
ImportError: No module named polls.tests


----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
Destroying test database for alias 'default'...

我的 INSTALLED_APPS 是:

INSTALLED_APPS = (
    'south',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

我做错了什么?

【问题讨论】:

  • syncdb 和 runserver 命令有效吗?
  • 是的,我的意思是他们运行没有错误
  • 遇到问题时您使用的是什么操作系统?我们无法在 Ubuntu 上重现它,但在 Mac 上有它
  • @sunprophit 操作系统是 ubuntu 服务器 14.04 但你可以看到它是前一段时间的 - 也许事情发生了变化

标签: python django django-unittest


【解决方案1】:

对于遇到相同问题的其他人,发生这种情况的另一个原因是您的根文件夹和项目文件夹的名称相同。

例如:

mydjango
├── __init__.py
├── manage.py
├── mydjango
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── polls
│   ├── admin.py
│   ├── __init__.py
│   ├── models.py
|   ├── tests.py
│   ├── templates

正在运行 ./manage.py test

抛出错误没有名为 polls.tests 的模块

要修复它,只需将根文件夹重命名为其他类似的名称:

mydjango_project
├── __init__.py
├── manage.py
├── mydjango
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── polls
│   ├── admin.py
│   ├── __init__.py
│   ├── models.py
|   ├── tests.py
│   ├── templates

【讨论】:

【解决方案2】:

随便跑

$ python manage.py test polls.tests

它有效,现在对我来说已经足够了:

Creating test database for alias 'default'...
F
======================================================================
FAIL: test_was_published_recently_with_future_poll (polls.tests.QuestionMethodTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/sergio/.virtualenvs/django4/mydjango/polls/tests.py", line 17, in test_was_published_recently_with_future_poll
    self.assertEqual(future_question.was_published_recently(), False)
AssertionError: True != False

【讨论】:

  • 虽然这是一个可行的解决方法,但我认为应该选择 pchiquet 的答案作为正确答案,因为它实际上解决了问题。
  • +1 接受的问题应该是@pchiquet 问题。
【解决方案3】:

我的 Django 项目遇到了完全相同的问题:

$ python manage test polls.tests

工作正常,而以下因导入错误而失败:

$ python manage test polls
$ python manage test
(...)
ImportError: Failed to import test module: mydjango.polls.tests
Traceback (most recent call last):
(...)
ImportError: No module named polls.tests

仔细检查错误信息:Django 的测试运行器尝试从 mydjango.polls.tests 导入测试,其中 mydjango 是根目录的名称(用于你的项目)。

我通过删除 mydjango 目录中的 __init__.py 文件(与 manage.py 文件处于同一级别)解决了这个问题。这个目录不应该是一个 python 模块,如果是这样的话,它似乎会与 Django 的测试运行程序混淆。

因此,只需删除 _init_.py 文件即可解决我们的问题

$ rm mydjango/__init__.py

【讨论】:

  • 另外,如果您按照 Django 1.9 教程进行操作,那么运行新单元测试的命令会出现拼写错误。确保运行 python manage.py test polls.tests 而不是记录在案的 python manage.py test polls
  • @DylanPierce 我很确定文档是正确的。只要我的项目文件夹中没有__init__.py,运行python manage.py test polls 对我有用。
  • 太好了,删除 init.py 文件为我做了。谢谢。
  • Lifesaver.. 谢谢“所以只需删除 init.py 文件就可以解决我们的问题:” 就像一个魅力.. :)
  • 天想知道为什么它不起作用,只是删除文件解决了问题。
【解决方案4】:

第一个答案对我不起作用。我用的是win8,可能是这个原因。 在终端尝试将 dir 更改为 ./polls 并运行

python ../manage.py test polls

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2019-11-27
    • 2021-01-02
    • 1970-01-01
    相关资源
    最近更新 更多