2016-08-02 56 views
0

我是tests in Django的新手。我需要写一对夫妇。编写和运行测试。 ImportError:没有模块命名为generic

Django版本1.9.7。 OS:Linux version 4.2.0-42-generic ([email protected]) (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 

我在做什么错?

+0

你可以发布你的动物模型吗? – levi

回答

1

您安装的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!') 
+0

我升级了django-autofixture,并且没有错误了。谢谢。 – trex

+0

感谢您提供有关'test_'前缀的提示! – trex

2

你打错进口,正确的进口主要是来自

django.contrib.contenttypes.fields import GenericRelation 

但这似乎实际上来自django自动夹具,而不是从您自己的代码。好消息是,你不需要自动夹具进行这种简单的测试。只要说再见吧。

+0

我升级了django-autofixture,没有错误了。你说我不必使用django-autofixture。那么,我必须使用什么呢? – trex

+1

其实没什么!例如,您所展示的测试不需要任何固定装置! – e4c5

相关问题