We’ve been using the django.contrib.auth app to handle all of our user login/logout/registration/etc. needs, and it has worked great for us so far. However, when we tried to run the unit tests for our project we found 10 or so errors with regards to the auth package itself.
At first, I thought it was because we were missing required templates, so I implemented Change Password… to see if I could make some of the unit tests pass. That didn’t work.
So after a bit more googling, I finally found a bug related to my problem: http://code.djangoproject.com/ticket/7756
It turns out that I was missing a required app in my setup.py’s INSTALLED_APP variable. Apparently, all you have to do is add django.contrib.admin to INSTALLED_APP make all the unit tests pass.
Sigh, well at least I got a new feature in!


Organizing Django Tests into Folders
Django comes with a nice testing rig that lets you individually test each app. By using a combination of pyunit & Django’s testing tools, you’ll be able to whip up a testing suite for each of your site’s apps (more information here: http://docs.djangoproject.com/en/dev/topics/testing/)
However there is some strangeness as to how Django expects the tests to be organized. From the Django documentation:
For a given Django application, the test runner looks for doctests in two places: * The models.py file. You can define module-level doctests and/or a doctest for individual models. It's common practice to put application-level doctests in the module docstring and model-level doctests in the model docstrings. * A file called tests.py in the application directory -- i.e., the directory that holds models.py. This file is a hook for any and all doctests you want to write that aren't necessarily related to models.Which sounds simple enough, until you end up with a bloated
tests.pyfile that covers 5 different aspects of your app.Now wouldn’t it be great if you could split them into different files and group them into a folder?
Putting your Tests into a Folder
teststests.pyfile into “logical” (what that exactly means is up to youimportstatements for each filetestsfoldertests, create a new file__init__.py__init__.pyas such:from [Project Name].[App Name].tests.[filename] import * #starts the test suite __test__= { 'mytest': [filename] }Note that the filename’s
.pyextension should be excluded from[filename]!__init__.py.
You can go ahead and delete the originaltests.pyfile now as well. Happy testing!