trex trex - 4 months ago 32
Python Question

Writing and running tests. ImportError: No module named generic

I'm new to tests in Django. And I need to write a couple.

Django version 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


My simple test code is:

cat animal/tests.py

from django.test import TestCase
from animal.models import Animal

class AnimalTestCase(TestCase):
def say_hello(self):
print('Hello, World!')


I execute it in this way
./manage.py test animal


And the following error arise:

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


What am I doing wrong?

Answer

Your installed version of django-autofixture does not support Django 1.9, because it has out of date imports for GenericRelation.

Try upgrading to the latest version. The project's changelist says that Django 1.9 support was added in version 0.11.0.

In order for Django to run your method in your AnimalTestCase, you need to rename it so that it begins with test_:

class AnimalTestCase(TestCase):
    def test_say_hello(self):
        print('Hello, World!')