When you develop a library, which should work with a number of Python versions, Tox is obvious choice. However, I start using it even in application developing, where single Python version is used. Because it helps me significantly reduce efforts of documentation writing. How? It transparently manages virtual environments.
For instance, you work on backend, and your colleague works on frontend. This guy is CSS ninja, but knows nothing about Python. So you have to explain him or her how to start the application in development mode. You can do this in two ways.
The first one is to write an instruction. The instruction should explain how to setup virtual environment, activate it, setup application in development mode, and run it. In 9 cases of 10 it blows non-pythonista’s mind away. Moreover, writing docs is tedious. Who likes writing docs, when you can write a script?!
And this is the second way. You can write a script, which creates virtual environment, activates it, installs application, and runs it. But this is exactly what Tox does.
Here is how I do it. The following tox.ini file is from the project I am working on now. It is a Pyramid application, which I test against Python 3.3 and 3.4, but develop using Python 3.4.
[tox] envlist=py33,py34 [testenv] deps= -r{toxinidir}/tests/requires.txt flake8 commands= nosetests flake8 application flake8 tests [testenv:dev] envdir=devenv basepython=python3.4 usedevelop=True deps= -r{toxinidir}/tests/requires.txt waitress commands={toxinidir}/scripts/devenv.sh {posargs}
Take a notice on the section [testenv:dev]. It launches devenv.sh script passing command line arguments, which are not processed by Tox itself. Here is the script:
#!/bin/bash test() { nosetests "$@" } serve() { pserve --reload "configs/development.ini" } cmd="$1" shift if [[ -n "$cmd" ]] then $cmd "$@" fi
And here is an example of the manual:
Install Tox.
To run application use:
$ tox -e dev serve
To run all tests using development environment:
$ tox -e dev test
To run single test using development environment:
$ tox -e dev test path/to/test.py
To run complete test suite and code linting:
$ tox
That’s it. Pretty simple. I copy it from project to project and my teammates are happy. You can even eliminate the first item from the list above, using Vagrant and installing Tox on provision stage. But there is a bug in Distutils, which breaks Tox within Vagrant. Use this hack to make it work.