Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Django custom model fields?

I am thinking of creating some subclassed Django model fields and distributing them as a package on PyPI. I like to write unit tests for my code (à la TDD), but I'm a bit puzzled as to how I'd write tests for this particular library.

The first thought that came to my mind was to create a Django project that makes use of my subclasses and just use the Django test tools, but that doesn't seem very elegant at all. There's got to be a better way!

Is there a method of somehow bootstrapping Django for this type of thing? I'd appreciate someone pointing me in the right direction. Thanks!

like image 973
arussell84 Avatar asked Feb 27 '12 21:02

arussell84


1 Answers

Django itself comes with some tests for field-subclassing; the tests have their own models.py where the custom fields are used. You should get the best impression when you have a look at the actual code yourself!

Addition: To have the models defined in your test package being discovered by django you will have to add your yourapp.test package to INSTALLED_APPS. Django itself has a built-in mechanism for its own tests to be automatically discovered and added to INSTALLED_APPS.

like image 106
Bernhard Vallant Avatar answered Oct 06 '22 14:10

Bernhard Vallant