Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 运行django教程测试失败-没有名为polls.tests的模块_Python_Django_Django Unittest - Fatal编程技术网

Python 运行django教程测试失败-没有名为polls.tests的模块

Python 运行django教程测试失败-没有名为polls.tests的模块,python,django,django-unittest,Python,Django,Django Unittest,我正在使用django 1.6教程,但无法运行测试。 我的项目(名为mydjango)和应用程序结构(名为polls)如下所示。(.nja文件是由我正在使用的忍者ide创建的) 我跟随教程了解django是如何工作的,但我被困在测试部分。 正如教程所建议的,我在app文件夹中创建了一个名为tests.py的文件,非常简单的文件是: # -*- coding: utf-8 -*- from django.test import TestCase import datetime from djang

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

我跟随教程了解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 = (
    'south',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)
我做错了什么?

不管怎样

$ 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

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

python ../manage.py test polls

我的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目录(与manage.py文件处于同一级别)中的
\uuuu init\uuuu.py
文件修复了此问题。这个目录不应该是python模块,如果是这样的话,它似乎会弄乱Django的测试运行程序

所以只要删除_init_uupy.py文件就可以解决我们的问题

$ rm mydjango/__init__.py

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

例如:

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测试

没有名为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

syncdb和runserver命令都能工作?是的,它们都能正常运行。我的意思是它们运行时没有错误。遇到问题时,您有什么操作系统?我们不能复制它在Ubuntu上,尽管我们已经打开了它Mac@sunprophit操作系统是ubuntu服务器14.04,但正如你所看到的,这是很久以前的事了——也许事情变了,但这是一个可行的解决方案,我认为应该选择pchiquet的答案作为正确答案,因为它实际上解决了问题。+1接受的问题应该是@pchiquet。或者,在Django 1.4+中,您应该删除
mydjango/uuu init_uuuuuu.py
,同时检查您是否遵循Django 1.9教程进行T,运行新单元测试的命令有一个输入错误。确保运行
python manage.py test polls.tests
而不是记录在案的
python manage.py test polls
@DylanPierce我很确定这些文档是正确的。运行
python manage.py test polls
对我来说很有效,只要我的项目文件夹中没有
\uuuu init\uuuuu.py
。谢谢,救命恩人。。谢谢“所以只要删除init.py文件就可以解决我们的问题:“工作得很有魅力…”几天不知道为什么它不起作用,只是删除文件修复了问题。
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