一尘不染

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

django

我正在玩django 1.6教程,但无法运行测试。我的项目(名称为mydjango)和应用程序结构(名称为民意调查)如下所示在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',
)

我究竟做错了什么?


阅读 492

收藏
2020-03-30

共1个答案

一尘不染

我的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文件处于同一级别)中的__init__.py文件来解决此问题。这个目录不应该是python模块,如果是这样的话,它似乎与Django的测试运行程序混淆了。

因此,仅删除_ init _.py文件应该可以解决我们的问题:

$ rm mydjango/__init__.py
2020-03-30