Django test model with FilerImageField - django

All new to Django, I want to write tests for an IndexView and a DetailView in analogy to the Django tutorial.
I have a model that contains a FilerImageField as mandatory field (blank=False).
In order to test my views related to that model, I want to create a model instance programmatically.
I am aware of this question addressing how to create a FilerImageField in code. The problem I run into applying the alleged solution is getting the part right about the image's owner.
def create_exhibitor(name, image_path, active):
filename = 'file'
user = User.objects.get(username='myuser')
with open(image_path) as f:
file_obj = File(f, name=filename)
image = Image.objects.create(
owner=user,
original_filename=filename,
file=file_obj
)
return Exhibitor(name=name, image=image, active=active)
Runnging them tests yields:
Traceback (most recent call last):
...
DoesNotExist: User matching query does not exist.
To me it appears there is no user in the test database.
So my question is twofold really:
Do I need a user there to create an instance of the model containing the FilerImageField?
If so, how do I create one for test purposes?

I'm finally doing it like so:
from django.test import TestCase
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from .models import Exhibitor
class TestCase():
su_username = 'user'
su_password = 'pass'
def setUp(self):
self.superuser = self.create_superuser()
def create_superuser(self):
return User.objects.create_superuser(self.su_username, 'email#example.com', self.su_password)
def create_exhibitor(self):
name = 'eins'
active = True
image_file = SimpleUploadedFile(
'monkey.jpg', b'monkey', content_type="image/jpeg"
)
return Exhibitor(name=name, image=image_file, active=active)

Related

Flask-Admin returns "ValueError: Invalid format string" on clicking create for any model

I have an existing Flask project which uses SQLAlchemy and I wanted to interate an Admin dashboard. Everything worked fine, I managed to enable authentication by using the ModelView Class however if I try to edit or if I try to create a new object of any database model then Flask-Admin throws out the following error:
ValueError: Invalid format string
Here's my Flask-Admin Code:
from flask_admin import Admin
from flask_login import current_user
from flask import redirect, url_for, request
from app import app, db, login
from flask_admin.contrib.sqla import ModelView
from app.auth.models import User
from app.forum.models import thread, post
from app.course.models import Courses
from flask_admin.model import typefmt
from datetime import date
app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'
def date_format(view, value):
return value.strftime('%d.%m.%Y')
MY_DEFAULT_FORMATTERS = dict(typefmt.BASE_FORMATTERS)
MY_DEFAULT_FORMATTERS.update({
type(None): typefmt.null_formatter,
date: date_format
})
class adminmodelview(ModelView):
column_type_formatters = MY_DEFAULT_FORMATTERS
def is_accessible(self):
return (current_user.is_authenticated and current_user.is_admin)
def inaccessible_callback(self, name, **kwargs):
return redirect(url_for('home.index'))
admin = Admin(app, name='celis', template_mode='bootstrap3')
admin.add_view(adminmodelview(User, db.session))
admin.add_view(adminmodelview(post, db.session))
admin.add_view(adminmodelview(thread, db.session))
admin.add_view(adminmodelview(Courses, db.session))
Here's the User Model:
class User(UserMixin,db.Model):
id=db.Column(db.Integer,primary_key=True)
username=db.Column(db.String(64),index=True,unique=True)
email=db.Column(db.String(120),index=True,unique=True)
user_role=db.Column(db.String(20))
is_admin=db.Column(db.Integer, default=0)
Region=db.Column(db.String(20))
password_hash=db.Column(db.String(128))
threads=db.relationship('thread',backref='creator',lazy='dynamic')
posts=db.relationship('post',backref='Author',lazy='dynamic')
last_seen=db.Column(db.DateTime,default=datetime.utcnow)
twitter=db.Column(db.String(120),default="N/A")
facebook=db.Column(db.String(120),default="N/A")
instagram=db.Column(db.String(120),default="N/A")
birthdate=db.Column(db.String(120),default="N/A")
Interests=db.Column(db.String(200),default="N/A")
provides_course=db.relationship('Courses',backref="Teacher",lazy='dynamic')
def __repr__(self):
return '<Role:{} Name:{} Id:{}>'.format(self.user_role,self.username,self.id)
def set_password(self,password):
self.password_hash=generate_password_hash(password)
def check_password(self,password):
return check_password_hash(self.password_hash,password)
def get_reset_token(self, expires_sec=1800):
s = Serializer(app.config['SECRET_KEY'], expires_sec)
return s.dumps({'id': self.id}).decode('utf-8')
On searching I found out it could be an issue due to the DateTime presentation, but could not figure out the solution.
bro I had a similar issue to yours, where the exception stemmed from the "date_posted" field in my "Threads" table as by default flask admin reads all data object as a String object so you have to override it as follows in your adminmodelview for example:
form_overrides=dict(date_posted=DateTimeField)

Unable to save model object

I have created a object which I am trying to save in Django admin in myapp>Hello. But the object does not get created under 'Hello' when I run the server. How can I fix it? I have also registered my models in admin.py.
models.py:
from django.db import models
from django.contrib.auth.models import User
class Foo(models.Model):
foo_id = models.CharField(max_length=10)
class Hello(models.Model):
user = models.ForeignKey(User, models.DO_NOTHING)
foo_id = models.ForeignKey('Foo', models.DO_NOTHING, db_column='foo_id')
foo_text = models.CharField(max_length=500, default="Hello!")
views.py
from django.shortcuts import render,HttpResponse,redirect
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from .models import User,Foo, Hello
from django.contrib.auth import settings
#login_required
def home(request):
return render(request, 'index.html')
#login_required
def sendhello() :
Foos=Foo.objects.all()
for foo in foos:
#Hello(user=user, foo_text='hello there', foo_id=foo).save()
xyz, obj=Hello.objects.get_or_create(user=user, foo_text='hello there', foo_id=foo)
if xyz is True:
obj.save()
#login_required
def helloxyz(request):
user = User.objects.get(id=request.session['id'])
hellos=Hello.objects.filter(user_id=user)
print(hellos)
hellos_list=[]
for hello in hellos:
print(hello.hello_text)
hellos_list.append(hello.hello_text)
hellos_list.reverse()
print(hellos_list)
return render(request,'index.html',{'hellos': hellos_list,})
First of all, your sendhello function is not a view, so it does not get called when you view your site. I will for now just assume you have a JS login dialogue or someting you get inputs from. SO you want to generate DB entries from these inputs that are present (?) in your DB. You could do this by simply calling your generating function from within a view class or function (I recommend to put such non-view helper functions in a seperate file and import them in your view) or outside of views with e.g. django cron.
The simplest would be to just create a new file utilis.py, import them in your views with import myapp.utils and then call your function at the top of your site view.
Why a sperate file? It is just for better readability, is cleaner when someone else looks at your code and swapping out helpers is a bit easier.
If you have regularly occuring tasks that should best be executed independant from the user loading a specific page, take a look at the django_crontab module, it is really handy. You need to be on a Linux system dough.
I hope that answered your question

How to pass a user instance in a class-based view

I would like to pass the user instance of the current user to a variable in a class-based view and feed it in a second step, within a python script, to a database table column defined as author = models.ForeignKey(User, on_delete=models.PROTECT). Related to that column I get the following integrity error: NOT NULL constraint failed.. From the traceback I can see that my variable loggedUser, which I had attributed self.request.user, is a SimpleLazyObject.
I have found another question similar to mine. But it discusses the issue related to functions - I am having difficulties to adapt the answers to my case of a class-based view.
How can I pass my user instance?
Thank you!
models.py
from django.db import models
from django.contrib.auth.models import User
class Data(models.Model):
temperature = models.FloatField(max_length=5)
author = models.ForeignKey(User, on_delete=models.PROTECT)
views.py
from . import apythonscript
from django.views.generic import TemplateView
class PlotGraphView(TemplateView):
def get_context_data(self, **kwargs):
loggedUser = self.request.user
context['plot'] = apythonscript.putDataInDB(loggedUser)
return context
apythonscript.py
from app.models import Data
def putDataInDB(loggedUser):
mydata=json.loads(response)
for line in mydata['feeds']:
# try:
data_handle = Data(temperature=line['field1'], author=loggedUser,)
data_handle.save()
# except:
# continue
traceback
link to traceback
to get the currently logged in user in Django, you can simply use, request.user. In CBV, request is a class object, so you should use self.request to access request data.
example
class PlotGraphView(LoginRequiredMixin, TemplateView):
# your stuff
def get_current_user(self): # this is just a custom function, you don't
return self.request.user
So, anywhere in the CBV's scope, you can access the current user by self.request.user

Import Class AUTH_USER_MODEL of custom User model into 3 party app

I need to import a custom User model into a third party app, problem is this app import django's default User model, so:
from django.contrib.auth.models import User
I have a custom User, I could do this:
from myapp.models import User
but I want a generic solution.
I had tried:
1:
from django.contrib.auth import get_user_model
User = get_user_model()
output:
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
2:
from django.conf import settings
from django.db.models import get_model
app_label, model_name = settings.AUTH_USER_MODEL.split('.')
User = get_model(app_label, model_name)
output:
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
3:
from django.conf import settings
User = settings.AUTH_USER_MODEL
output:
TypeError
isinstance() arg 2 must be a class, type, or tuple of classes and types
specifically need execute this code
if isinstance(author, User) and author.is_authenticated():
kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email)
elif isinstance(author, six.string_types):
kwargs['author'] = author
How I could do it?
Thanks
I think that your "approach 1" is the right one, but the problem is that you can't run get_user_model() until after everything has been loaded. For example, you could change the main code to:
if isinstance(author, get_user_model()) and author.is_authenticated():
kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email)
elif isinstance(author, six.string_types):
kwargs['author'] = author
That would be slow, since it runs get_user_model() each time, so you should find a place to keep the user model so that you only run get_user_model() the first time the code is called. For example, if this code is in a method of an object, you could put self._user_model = get_user_model() in the constructor.
Another option would be to check if the author is a string_types first, and then
try:
kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email)
except TypeError:
# do whatever you already do, if the client code passes the wrong kind of object.
pass

Django: Best way to unit-test an abstract model

I need to write some unit tests for an abstract base model, that provides some basic functionality that should be used by other apps. It would be necessary to define a model that inherits from it just for testing purposes. Are there any elegant/simple ways to define that model just for testing?
I have seen some "hacks" that make this possible, but never seen an "official" way in the Django documentation or in other similar places.
Just stumbled across this feature myself: You can just inherit from your abstract model in tests.py and test that as usual. When you run 'manage.py tests', Django not only creates a test database, but also validates & syncs your test models.
Tested it with current Django trunk (version 1.2).
Updated for Django >=2.0
So I was running into a few problems using m4rk4l's answer: one being the 'RuntimeWarning: Model 'myapp.__test__mymodel' was already registered' issue brought up in one of the comments, another being tests failing because the table already exists.
I've added a few checks to help solve these issues and now it works flawlessly. I hope this helps people
from django.db import connection
from django.db.models.base import ModelBase
from django.db.utils import OperationalError
from django.test import TestCase
class AbstractModelMixinTestCase(TestCase):
"""
Base class for tests of model mixins/abstract models.
To use, subclass and specify the mixin class variable.
A model using the mixin will be made available in self.model
"""
#classmethod
def setUpTestData(cls):
# Create a dummy model which extends the mixin. A RuntimeWarning will
# occur if the model is registered twice
if not hasattr(cls, 'model'):
cls.model = ModelBase(
'__TestModel__' +
cls.mixin.__name__, (cls.mixin,),
{'__module__': cls.mixin.__module__}
)
# Create the schema for our test model. If the table already exists,
# will pass
try:
with connection.schema_editor() as schema_editor:
schema_editor.create_model(cls.model)
super(AbstractModelMixinTestCase, cls).setUpClass()
except OperationalError:
pass
#classmethod
def tearDownClass(self):
# Delete the schema for the test model. If no table, will pass
try:
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(self.model)
super(AbstractModelMixinTestCase, self).tearDownClass()
except OperationalError:
pass
To use, implement the same way as above (now with the correcting indentation):
class MyModelTestCase(AbstractModelMixinTestCase):
"""Test abstract model."""
mixin = MyModel
def setUp(self):
self.model.objects.create(pk=1)
def test_a_thing(self):
mod = self.model.objects.get(pk=1)
I have the same situation as well. I ended up using a version of #dylanboxalot solution. Got extra details from here specifically after reading 'Test structure overview' section.
The setUp and the tearDown methods are called each time a tests is run. A better solution is to run the creation of the 'abstract' model once, before all the tests are run. To do so, you can implement the setUpClassData and also implement the tearDownClass.
class ModelMixinTestCase(TestCase):
'''
Base class for tests of model mixins. To use, subclass and specify the
mixin class variable. A model using the mixin will be made available in
self.model
'''
#classmethod
def setUpClass(cls):
# Create a dummy model which extends the mixin
cls.model = ModelBase('__TestModel__' +
cls.mixin.__name__, (cls.mixin,),
{'__module__': cls.mixin.__module__}
)
# Create the schema for our test model
with connection.schema_editor() as schema_editor:
schema_editor.create_model(cls.model)
super(ModelMixinTestCase, cls).setUpClass()
#classmethod
def tearDownClass(cls):
# Delete the schema for the test model
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(cls.model)
super(ModelMixinTestCase, cls).tearDownClass()
A possible implementation may look like this:
class MyModelTestCase(ModelMixinTestCase):
mixin = MyModel
def setUp(self):
# Runs every time a test is run.
self.model.objects.create(pk=1)
def test_my_unit(self):
# a test
aModel = self.objects.get(pk=1)
...
Maybe ModelMixinTestCase class should be added to Django? :P
I stumbled across this recently and wanted to update it for newer Django versions (1.9 and later) You can use the SchemaEditor's create_model instead of the outdated sql_create_model
from django.db import connection
from django.db.models.base import ModelBase
from django.test import TestCase
class ModelMixinTestCase(TestCase):
"""
Base class for tests of model mixins. To use, subclass and specify
the mixin class variable. A model using the mixin will be made
available in self.model.
"""
def setUp(self):
# Create a dummy model which extends the mixin
self.model = ModelBase('__TestModel__' + self.mixin.__name__, (self.mixin,), {'__module__': self.mixin.__module__})
# Create the schema for our test model
with connection.schema_editor() as schema_editor:
schema_editor.create_model(self.model)
def tearDown(self):
# Delete the schema for the test model
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(self.model)
I think what you are looking for is something like this.
This is the full code from the link:
from django.test import TestCase
from django.db import connection
from django.core.management.color import no_style
from django.db.models.base import ModelBase
class ModelMixinTestCase(TestCase):
"""
Base class for tests of model mixins. To use, subclass and specify
the mixin class variable. A model using the mixin will be made
available in self.model.
"""
def setUp(self):
# Create a dummy model which extends the mixin
self.model = ModelBase('__TestModel__'+self.mixin.__name__, (self.mixin,),
{'__module__': self.mixin.__module__})
# Create the schema for our test model
self._style = no_style()
sql, _ = connection.creation.sql_create_model(self.model, self._style)
self._cursor = connection.cursor()
for statement in sql:
self._cursor.execute(statement)
def tearDown(self):
# Delete the schema for the test model
sql = connection.creation.sql_destroy_model(self.model, (), self._style)
for statement in sql:
self._cursor.execute(statement)
Develop a minimal example app that you distribute with your 'abstract' models.
Provide tests for the example app to prove the abstract models.
Maikhoepfel's answer is correct and most of the others seem unnecessarily complex. I wanted to provide further clarification as the other more complex answers seem to be quite popular.
project/
├─ app1/
├─ app2/
│ ├─ tests/
│ │ ├─ __init__.py
│ │ ├─ models.py
│ │ ├─ test_models.py
│ ├─ __init__.py
│ ├─ apps.py
│ ├─ models.py
Given the above project structure a model in app2.tests.models that inherits from app2.models.YourAbstractModel will be available for use in any tests (eg. app2.tests.test_models) without the need to run migrations.
Examples of this can be seen in Django test source code.
I came to this problem my self and my solution is on this gist django-test-abstract-models
you can use it like this:
1- subclass your django abstract models
2- write your test case like this:
class MyTestCase(AbstractModelTestCase):
self.models = [MyAbstractModelSubClass, .....]
# your tests goes here ...
3- if you didn't provide self.models attribute it will search the current app for models in the path myapp.tests.models.*
In Django 2.2, if you only have one abstract class to test, you can use the following:
from django.db import connection
from django.db import models
from django.db.models.base import ModelBase
from django.db.utils import ProgrammingError
from django.test import TestCase
from yourapp.models import Base # Base here is the abstract model.
class BaseModelTest(TestCase):
#classmethod
def setUpClass(cls):
# Create dummy model extending Base, a mixin, if we haven't already.
if not hasattr(cls, '_base_model'):
cls._base_model = ModelBase(
'Base',
( Base, ),
{ '__module__': Base.__module__ }
)
# Create the schema for our base model. If a schema is already
# create then let's not create another one.
try:
with connection.schema_editor() as schema_editor:
schema_editor.create_model(cls._base_model)
super(BaseModelTest, cls).setUpClass()
except ProgrammingError:
# NOTE: We get a ProgrammingError since that is what
# is being thrown by Postgres. If we were using
# MySQL, then we should catch OperationalError
# exceptions.
pass
cls._test_base = cls._base_model.objects.create()
#classmethod
def tearDownClass(cls):
try:
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(cls._base_model)
super(BaseModelTest, cls).tearDownClass()
except ProgrammingError:
# NOTE: We get a ProgrammingError since that is what
# is being thrown by Postgres. If we were using
# MySQL, then we should catch OperationalError
# exceptions.
pass
This answer is only a tweaking of DSynergy's answer. One notable difference is that we are using setUpClass() instead of setUpTestData(). This difference is important since using the latter will result in InterfaceError (when using PostgreSQL) or the equivalent in other databases when the other test cases are run. As to the reason why this happens, I do not know at the time of writing.
NOTE: If you have more than one abstract class to test, it is better to use the other solutions.
I tried solutions here but ran into issues like
RuntimeWarning: Model 'myapp.__test__mymodel' was already registered
Looking up how to test abstract models with pytest wasn't any successful either. I eventually came up with this solution that works perfectly for me:
import tempfile
import pytest
from django.db import connection, models
from model_mommy import mommy
from ..models import AbstractModel
#pytest.fixture(scope='module')
def django_db_setup(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
class DummyModel(AbstractModel):
pass
class DummyImages(models.Model):
dummy = models.ForeignKey(
DummyModel, on_delete=models.CASCADE, related_name='images'
)
image = models.ImageField()
with connection.schema_editor() as schema_editor:
schema_editor.create_model(DummyModel)
schema_editor.create_model(DummyImages)
#pytest.fixture
def temporary_image_file():
image = tempfile.NamedTemporaryFile()
image.name = 'test.jpg'
return image.name
#pytest.mark.django_db
def test_fileuploader_model_file_name(temporary_image_file):
image = mommy.make('core.dummyimages', image=temporary_image_file)
assert image.file_name == 'test.jpg'
#pytest.mark.django_db
def test_fileuploader_model_file_mime_type(temporary_image_file):
image = mommy.make('core.dummyimages', image=temporary_image_file)
assert image.file_mime_type == 'image/jpeg'
As you can see, I define a Class that inherits from the Abstractmodel, and add it as a fixture.
Now with the flexibility of model mommy, I can create a DummyImages object, and it will automatically create a DummyModel for me too!
Alternatively, I could've made the example simple by not including foreign keys, but it demonstrates the flexibility of pytest and model mommy in combination quite well.
I thought I could share with you my solution, which is in my opinion much simpler and I do not see any cons.
Example goes for using two abstract classes.
from django.db import connection
from django.db.models.base import ModelBase
from mailalert.models import Mailalert_Mixin, MailalertManager_Mixin
class ModelMixinTestCase(TestCase):
#classmethod
def setUpTestData(cls):
# we define our models "on the fly", based on our mixins
class Mailalert(Mailalert_Mixin):
""" For tests purposes only, we fake a Mailalert model """
pass
class Profile(MailalertManager_Mixin):
""" For tests purposes only, we fake a Profile model """
user = models.OneToOneField(User, on_delete=models.CASCADE,
related_name='profile', default=None)
# then we make those models accessible for later
cls.Mailalert = Mailalert
cls.Profile = Profile
# we create our models "on the fly" in our test db
with connection.schema_editor() as editor:
editor.create_model(Profile)
editor.create_model(Mailalert)
# now we can create data using our new added models "on the fly"
cls.user = User.objects.create_user(username='Rick')
cls.profile_instance = Profile(user=cls.user)
cls.profile_instance.save()
cls.mailalert_instance = Mailalert()
cls.mailalert_instance.save()
# then you can use this ModelMixinTestCase
class Mailalert_TestCase(ModelMixinTestCase):
def test_method1(self):
self.assertTrue(self.mailalert_instance.method1())
# etc
Here is a working solution in django 3.0 with Postgres. It allows testing any number of abstract models and also maintains any integrity related to foreign objects.
from typing import Union
from django.test import TestCase
from django.db import connection
from django.db.models.base import ModelBase
from django.db.utils import ProgrammingError
# Category and Product are abstract models
from someApp.someModule.models import Category, Product, Vendor, Invoice
class MyModelsTestBase(TestCase):
#classmethod
def setUpTestData(cls):
# keep track of registered fake models
# to avoid RuntimeWarning when creating
# abstract models again in the class
cls.fake_models_registry = {}
def setUp(self):
self.fake_models = []
def tearDown(self):
try:
with connection.schema_editor(atomic=True) as schema_editor:
for model in self.fake_models:
schema_editor.delete_model(model)
except ProgrammingError:
pass
def create_abstract_models(self, models: Union[list, tuple]):
"""
param models: list/tuple of abstract model class
"""
# by keeping model names same as abstract model names
# we are able to maintain any foreign key relationship
model_names = [model.__name__ for model in models]
modules = [model.__module__ for model in models]
for idx, model_name in enumerate(model_names):
# if we already have a ModelBase registered
# avoid re-registering.
registry_key = f'{modules[idx]}.{model_name}'
model_base = self.fake_models_registry.get(registry_key)
if model_base is not None:
self.fake_models.append(model_base)
continue
# we do not have this model registered
# so register it and track it in our
# cls.fake_models_registry
self.fake_models.append(
ModelBase(
model_name,
(models[idx],),
{'__module__': modules[idx]}
)
)
self.fake_models_registry[registry_key] = self.fake_models[idx]
errors = []
# atomic=True allows creating multiple models in the db
with connection.schema_editor(atomic=True) as schema_editor:
try:
for model in self.fake_models:
schema_editor.create_model(model)
except ProgrammingError as e:
errors.append(e)
pass
return errors
def test_create_abstract_models(self):
abstract_models = (Category, Product)
errors = self.create_abstract_models(abstract_models)
self.assertEqual(len(errors), 0)
category_model_class, product_model_class = self.fake_models
# and use them like any other concrete model class:
category = category_model_class.objects.create(name='Pet Supplies')
product = product_model_class.objects.create(
name='Dog Food', category_id=category.id
)
Having read through all the answers above, I found out a solution that worked for me, in Django 3.1.1 with PostgreSQL 12.4 database.
from django.db import connection
from django.db.utils import ProgrammingError
from django.test import TestCase
class AbstractModelTestCase(TestCase):
"""
Base class for tests of model mixins. To use, subclass and specify the
mixin class variable. A model using the mixin will be made available in
self.model
"""
#classmethod
def setUpClass(cls):
if not hasattr(cls, "model"):
super(AbstractModelTestCase, cls).setUpClass()
else:
# Create the schema for our test model. If the table already exists, will pass
try:
with connection.schema_editor() as schema_editor:
schema_editor.create_model(cls.model)
super(AbstractModelTestCase, cls).setUpClass()
except ProgrammingError:
pass
#classmethod
def tearDownClass(cls):
if hasattr(cls, "model"):
# Delete the schema for the test model
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(cls.model)
super(AbstractModelTestCase, cls).tearDownClass()
It also gets rid of the annoying RuntimeWarning: Model 'xxx' was already registered warning.
Testing an abstract class is not too useful, as a derived class can override its methods. The other applications are responsible for testing their classes based on your abstract class.