Writing and running tests. ImportError: No module named generic
Writing and running tests. ImportError: No module named generic
我是 tests in Django 的新手。我需要写一对。
Django 版本 1.9.7。
OS: Linux version 4.2.0-42-generic (buildd@lgw01-54) (gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2) ) #49-Ubuntu SMP Tue Jun 28 21:26:26 UTC 2016
我的简单测试代码是:
cat animal/tests.py
from django.test import TestCase
from animal.models import Animal
class AnimalTestCase(TestCase):
def say_hello(self):
print('Hello, World!')
我是这样执行的./manage.py test animal
并出现以下错误:
Traceback (most recent call last):
File "./manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/path-to-venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/path-to-venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 327, in execute
django.setup()
File "/path-to-venv/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/path-to-venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/path-to-venv/local/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/path-to-venv/local/lib/python2.7/site-packages/autofixture/__init__.py", line 5, in <module>
from autofixture.base import AutoFixture
File "/path-to-venv/local/lib/python2.7/site-packages/autofixture/base.py", line 7, in <module>
from django.contrib.contenttypes.generic import GenericRelation
ImportError: No module named generic
我做错了什么?
您安装的 django-autofixture 版本不支持 Django 1.9,因为它的 GenericRelation
.
导入已过时
尝试升级到最新版本。该项目的 changelist 表示在版本 0.11.0 中添加了 Django 1.9 支持。
为了让 Django 在您的 AnimalTestCase
中 运行 您的方法,您需要重命名它,使其以 test_
:
开头
class AnimalTestCase(TestCase):
def test_say_hello(self):
print('Hello, World!')
你导入错误,正确的导入来自
django.contrib.contenttypes.fields import GenericRelation
但这似乎实际上来自 django auto-fixtures 而不是您自己的代码。好消息是您不需要自动夹具来进行这种简单的测试。就跟它说再见吧。
我是 tests in Django 的新手。我需要写一对。
Django 版本 1.9.7。
OS: Linux version 4.2.0-42-generic (buildd@lgw01-54) (gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2) ) #49-Ubuntu SMP Tue Jun 28 21:26:26 UTC 2016
我的简单测试代码是:
cat animal/tests.py
from django.test import TestCase
from animal.models import Animal
class AnimalTestCase(TestCase):
def say_hello(self):
print('Hello, World!')
我是这样执行的./manage.py test animal
并出现以下错误:
Traceback (most recent call last):
File "./manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/path-to-venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/path-to-venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 327, in execute
django.setup()
File "/path-to-venv/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/path-to-venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/path-to-venv/local/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/path-to-venv/local/lib/python2.7/site-packages/autofixture/__init__.py", line 5, in <module>
from autofixture.base import AutoFixture
File "/path-to-venv/local/lib/python2.7/site-packages/autofixture/base.py", line 7, in <module>
from django.contrib.contenttypes.generic import GenericRelation
ImportError: No module named generic
我做错了什么?
您安装的 django-autofixture 版本不支持 Django 1.9,因为它的 GenericRelation
.
尝试升级到最新版本。该项目的 changelist 表示在版本 0.11.0 中添加了 Django 1.9 支持。
为了让 Django 在您的 AnimalTestCase
中 运行 您的方法,您需要重命名它,使其以 test_
:
class AnimalTestCase(TestCase):
def test_say_hello(self):
print('Hello, World!')
你导入错误,正确的导入来自
django.contrib.contenttypes.fields import GenericRelation
但这似乎实际上来自 django auto-fixtures 而不是您自己的代码。好消息是您不需要自动夹具来进行这种简单的测试。就跟它说再见吧。