views:

514

answers:

2

Hi!

I come across several problems while trying django unittests library. Something strange happens:

I defined the test like this:

from django.core import management
from django.test import TestCase
from django.test.client import Client
from django.core import mail
from django.test.utils import setup_test_environment
from django.contrib.auth.models import User
from django.db import connection
from goserver.models import ActiveList

class GoserverTestCase(TestCase):
    #fixtures = ['dat.json']

    def setUp(self):
        pass

    def test_active_list_works(self):
        c = Client()
        response = c.post('/')
        #print response.status_code
        self.assertEquals(True, True)

But after the execution of the code it returns following error:

----------------------------------------------------------------------  Unit Test Code Coverage Results
---------------------------------------------------------------------- Traceback (most recent call last):   File "manage.py", line 11, in <module>
    execute_manager(settings)   File "/opt/local/lib/python2.5/site-packages/Django-1.0.2_final-py2.5.egg/django/core/management/__init__.py", line 340, in execute_manager
    utility.execute()   File "/opt/local/lib/python2.5/site-packages/Django-1.0.2_final-py2.5.egg/django/core/management/__init__.py", line 295, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv) File "/opt/local/lib/python2.5/site-packages/Django-1.0.2_final-py2.5.egg/django/core/management/base.py", line 192, in run_from_argv
    self.execute(*args, **options.__dict__)   File "/opt/local/lib/python2.5/site-packages/Django-1.0.2_final-py2.5.egg/django/core/management/base.py", line 219, in execute
    output = self.handle(*args, **options)   File "/opt/local/lib/python2.5/site-packages/Django-1.0.2_final-py2.5.egg/django/core/management/commands/test.py", line 33, in handle
    failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive)   File "/opt/local/lib/python2.5/site-packages/django_test_coverage-0.1-py2.5.egg/django-test-coverage/runner.py", line 58, in run_tests
    modules.extend(_package_modules(*pkg)) File "/opt/local/lib/python2.5/site-packages/django_test_coverage-0.1-py2.5.egg/django-test-coverage/runner.py", line 92, in _package_modules
    modules.append(__import__(impstr + '.' + name, {}, {}, ['']))   File "/Users/oleg/jin/goclub/trunk/jin/goserver/admin.py", line 11, in <module>
    admin.site.register(ActiveList, ActiveListAdmin)   File "/opt/local/lib/python2.5/site-packages/Django-1.0.2_final-py2.5.egg/django/contrib/admin/sites.py", line 64, in register
    raise AlreadyRegistered('The model %s is already registered' % model.__name__) django.contrib.admin.sites.AlreadyRegistered: The model ActiveList is already registered silver:jin oleg$

Admin file looks like this:

from goserver.models import ActiveList, Game
from django.contrib import admin

class ActiveListAdmin(admin.ModelAdmin):
    list_display = ('user', "is_Bot", "isActive")

admin.site.register(ActiveList, ActiveListAdmin)
admin.site.register(Game)

I run it all this way: python manage.py test goserver

Also noticed that if I remove lines c = Client() response = c.post('/')

from a test case definition, then no error appears

A: 

My questions,

  • I don't see what is base type/class for TestCase - is it Django Test one, or from Unittest? it is better to use from Django
  • How are you runnig test? using Django internal test command, by nose, by unittest? By Traceback I thing test command, but I am not quite sure.
  • What is you definitions for ActiveAdminList and ActiveList? Have you got maybe class Admin in Meta?
bluszcz
Thanks for answer! I added needed info to original description
Oleg Tarasenko
Can you paste content of the ActiveList model?, I think that there could be a problem.
bluszcz
+1  A: 

Looking at the traceback, it looks like you have an app called django_test_coverage-0.1 which is importing your app's admin.py.

It is probably importing it from a different location, such as yourproject.yourapp.admin as opposed to yourapp.admin. Since it's technically seen as a different module, it is re-imported and the admin.site.register calls are made again. This causes the AlreadyRegistered error.

My suggestion would be to remove django_test_coverage app (or fix it).

SmileyChris