Using Sublime Text Build System for Project Unit Tests

I spent some time today learning about the Build System. In addition to the built-in build systems for various languages, and the possibility of defining custom build systems at the user preference level, you can also specify project build systems.

I added the following stanza to one of my .sublime-project files:

         "name": "unittest-discover",
         "shell_cmd": "~/.virtualenvs/randopony-tetra-2.7/bin/python
                       -m unittest discover ${project_path}"

to enable me to run the test suite for the project within Sublime at the touch of a key.

Things to note:

  • The name element defines how the build will appear in the Tools > Build System menu.
  • In the shell_cmd element:
    • I use an explicit path to the python interpreter in the project's virtualenv so that the project's dependencies are found correctly.
    • The shell that the Sublime build command launches will have as its cwd the directory that the file you initiate the build command from is in. ${project_path} is a build system variable that points to the directory where the .sublime-project file is, and I use that to tell unittest discover where to start searching for tests.
  • You can define as many build systems as you want in the .sublime-project file, just be sure to give them different names.

After reloading the .sublime-project file, choose unittest-discover from the Tools > Build System menu, and launch a build (⌘B on OS/X), and voilà, the test suite runs in a pane that pops up at the bottom of the Sublime window.

blog comments powered by Disqus