{"id":9,"date":"2008-09-13T15:10:32","date_gmt":"2008-09-13T13:10:32","guid":{"rendered":"http:\/\/daniel.hepper.net\/blog\/?p=9"},"modified":"2016-04-24T12:55:58","modified_gmt":"2016-04-24T11:55:58","slug":"pitfalls-in-django-unittests","status":"publish","type":"post","link":"https:\/\/daniel.hepper.net\/blog\/2008\/09\/pitfalls-in-django-unittests\/","title":{"rendered":"Pitfalls in Django unittests"},"content":{"rendered":"<p>I&#8217;m currently rewriting dwidder\u00a0[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:<\/p>\n<ol>\n<li><strong>Write fixtures<\/strong>. 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.<\/li>\n<li><strong>Don&#8217;t subclass unittest.TestCase<\/strong>. Use Django&#8217;s TestCase class instead. Otherwise you will miss all the fancy stuff, like automatic fixture loading.<\/li>\n<li><strong>Import mail, not outbox.<\/strong> 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.<\/li>\n<\/ol>\n<p>[1] a now defunct site where you could tweet by sending an SMS to a German mobile phone number<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m currently rewriting dwidder\u00a0[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 &hellip; <a href=\"https:\/\/daniel.hepper.net\/blog\/2008\/09\/pitfalls-in-django-unittests\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[33,5,6],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-django","tag-django","tag-python","tag-testing"],"_links":{"self":[{"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":4,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/posts\/9\/revisions\/547"}],"wp:attachment":[{"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}