Django: Best way to unit-test an abstract model

Bernhard Vallant picture Bernhard Vallant · Nov 26, 2010 · Viewed 7.2k times · Source

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 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.

Answer

maikhoepfel picture maikhoepfel · Dec 1, 2010

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).