I wanted to run Python unit tests using different Python versions on both Linux and Windows. The unit tests had a lot of dependencies. While looking into quick wins for getting the unit tests running via Buildbot, a continuous integration system, I found the pot of gold – Tox.
What does Tox do?
- Creates a virtual environment for the Python versions you want to run your tests against by using virtualenv.
- In the virtualenv, all the dependencies and packages required for the tests are installed.
- Then the sequence of tests is run in the virtual environments created.
Setting up tox is very simple. You can install tox with pip install tox or easy_install tox. A config file ‘tox.ini‘ with the information about your project and environments should be created. If you have setup.py, the tox.ini file should reside next to it.
An example tox.ini looks like this:
By running the following command, test.py is run on both Python 2.7 and 2.6.
Learn more about tox configuration and usage examples from the official tox documentation.