Run Django Unit Tests in a Sublime Text 2 Build System

With the dev builds of Sublime Text 2, you can easily set up a build system that runs Django unit tests. You do this by adding a build system to your Sublime Text 2 project file for the Django project.

I’ll include an example project file in this post that runs manage.py test --noinput as the build command. In order for this to work with the current implementation of build systems, the project file must add the Django project dir and the virtualenv’s site-packages directory to the PYTHONPATH.

Note a couple of things:

  • In the project file, ${project_path} refers to the directory in which the project file exists: I created mine in the virtual env directory (the directory with the bin/ and lib/ directores for the virtualenv). For more info on other possible substitutions see the docs (note that I couldn’t get substitutions to work in the “env” dictionary)
  • My placeholder text django_project_dir stands for the directory that contains your Django project files inside of the virtualenv
  • This doesn’t use the python binary in your virtualenv — so your mileage may vary

One final thing to note is that I’ve added the lib/python2.7 directory as a folder in the project file. This is not related to the build system. It simply includes libraries in my “Find in File…” searches, allowing me to easily search for, e.g., Django classes and usages alongside my own. (SublimeRope is also a helpful tool for exploring.)

Anyway, here is the example project file: