Tox-Driven Python Development

Going through Julian Berman’s jsonschema repo (some of the cleanest Python code I’ve read, by the way), I stumbled upon a recommendation that greatly improved my Python workflow. I’m talking about Tox, the Python testing automaton.

Tox allows you to test your code against multiple versions of Python (yes, PyPy included!) with a single command:

$ tox

It uses virtualenv to create a virtual Python environment for each Python version you want to support, installs your dependencies as well as the package you are currently developing in each of these enviroments and runs a sequence of testing commands of your choice in each environment.

Tox is great for Test-driven development (TDD). In fact, I no longer need to activate a virtual environment during development. My workflow now looks like this:

  1. Write a test
  2. Write some code
  3. Run tox and tweak code until test passes

If you think your code works on all versions of Python just because it works on your development version, think again!

Set-up

install_requires = [
    "jsonschema==0.7-patched",
],
dependency_links = [
    # Patched version to support a special feature
    "http://github.com/boronine/jsonschema/tarball\
/391916e71b946db3bf5d92cbb767f620a869fc82#egg=jsonschema-0.7-patched"
]
include tox.ini
# If your tests aren't part of the distributed package, make sure to include them
recursive-include tests *
[tox]
envlist = py26, py27, pypy

[testenv]
deps =
    nose
    unittest2
commands =
    nosetests

Good luck!