我正在玩 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',
)
我究竟做错了什么?