I’m currently rewriting dwidder [1] and I just spent way to much time on a unit test for the registration view. These are the things that made me stumble:
- Write fixtures. Unit tests run on an empty database. Somewhere deep in your code might be a reference to one of the default applications, like django.contrib.sites, which will fail, because there is no default site object in your database.
- Don’t subclass unittest.TestCase. Use Django’s TestCase class instead. Otherwise you will miss all the fancy stuff, like automatic fixture loading.
- Import mail, not outbox. Django provides a dummy email outbox. But to make it work as expected, you have to import the mail module. If you import the outbox directly, it will always be empty.
[1] a now defunct site where you could tweet by sending an SMS to a German mobile phone number