Python 运行 django 教程测试失败 - 没有名为 polls.tests 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21069880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running django tutorial tests fail - No module named polls.tests
提问by bre
I'm playing with django 1.6 tutorial but i can't run tests. My project (name mydjango) and app structure (name is polls) are as shown below in a virtualenv. (.nja files are just created by ninja-ide the ide I'm using)
我正在玩 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
I followed the tutorial to understand how django works but I'm stuck in the test part. As tutorial suggest I created a file named tests.py into the app folder, the pretty straightforward file is:
我按照教程来了解 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)
then i installed unittest2 into the virtualenv with
然后我将 unittest2 安装到 virtualenv 中
$pip install unittest2
and run
并运行
$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'...
No way to have the test working, also if don't pass the app name it returns the same error:
无法让测试工作,如果不传递应用程序名称,它也会返回相同的错误:
$ 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'...
My INSTALLED_APPS are:
我的 INSTALLED_APPS 是:
INSTALLED_APPS = (
'south',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
What am I doing wrong?
我究竟做错了什么?
采纳答案by pchiquet
I had exactly the same issue with my Django project:
我的 Django 项目遇到了完全相同的问题:
$ python manage test polls.tests
worked fine whereas the following failed with an import error:
工作正常,而以下因导入错误而失败:
$ 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
Check carefully the error message: Django's test runner tries to import the tests from mydjango.polls.testswhere mydjangois the name of the root directory (the container for your project).
仔细检查错误消息:Django 的测试运行程序尝试从mydjango.polls.tests导入测试,其中mydjango是根目录(项目的容器)的名称。
I fixed this issue by deleting the __init__.pyfile in mydjangodirectory (at the same level than manage.py file). This directory is not supposed to be a python module and it seems to mess up with Django's test runner if it is the case.
我通过删除mydjango目录中的__init__.py文件(与 manage.py 文件处于同一级别)来解决此问题。这个目录不应该是一个 python 模块,如果是这样的话,它似乎会与 Django 的测试运行程序混淆。
So just deleting the _init_.py file should fix our problem:
所以只要删除 _ init_.py 文件就可以解决我们的问题:
$ rm mydjango/__init__.py
回答by bre
Anyhow running
无论如何运行
$ python manage.py test polls.tests
It works, it's enough for me right now:
它有效,现在对我来说已经足够了:
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
回答by mazdack
first answer didn't work for me. im using win8, may be this is a reason. in terminal try to change dir to ./polls and the run
第一个答案对我不起作用。我用的是win8,可能是这个原因。在终端中尝试将 dir 更改为 ./polls 并运行
python ../manage.py test polls
回答by buluba89
For anyone else having the same problem, another reason for this to happen is if you have the same name for the root folder and the project folder.
对于遇到同样问题的其他人,发生这种情况的另一个原因是根文件夹和项目文件夹的名称相同。
For example:
例如:
mydjango
├── __init__.py
├── manage.py
├── mydjango
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
├── polls
│ ├── admin.py
│ ├── __init__.py
│ ├── models.py
| ├── tests.py
│ ├── templates
running
./manage.py test
跑步
./manage.py test
throws errors No module named polls.tests
抛出错误 没有名为 polls.tests 的模块
to fix it simply rename the root folder to something else like :
要修复它,只需将根文件夹重命名为其他名称,例如:
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

