How to Use Tox to Automate Environment Management and to Autorun Python Tests?

Praveen NG
4 min readJul 20, 2021

Tox is a generic virtual environment manager. With Tox it easy to set up different environment and run different commands in different environments. It is typically used for running automated tests, but it can be used for other purposes as well. I started to use Tox recently and found it a very handy tool. However, I could not find a good tutorial or user guide describing the basic concepts and it took me a while to understand the concepts. In this post, I am attempting to describe the basics of how Tox works.

Below I create three environments with two different NumPy versions and two python files.

env1 — main1.py running with NumPy 1.20.0
env2 — main1.py running with NumPy 1.21.0
env3 — main1.py and main2.py running with NumPy 1.20.0

All these details are described in the Tox configuration file called tox.ini. Once the configuration file is created, it is easy to run any one or more of these files in these environments. So you can imagine how easy it would be run different testing codes, type checks etc. One thing to notice is that even though

Installation

The easiest (and the best) way to install Tox is using pip.

pip install tox

Config File (tox.ini)

To get started, Tox requires a configuration file called tox.ini. Below is the tox.ini file I created. The first two sections [tox] and [testenv] describe general configurations. One can describe, e.g., which environments and commands should run by default if you don’t specify an environment. I’m skipping it here, so by default tox will create all listed environments and run the listed commands within them. skipsdist is set to True because otherwise tox will look for another file setup.py, which is not needed for our purposes.

tox.ini

An environment env1 is described next with [testenv:env1]. The dependencies for this environment are listed next. In this case, it is only NumPy version 1.20.0. Next the commands to run are described. In our case, we have only one command to run main1.py within env1.

main1.py

Next we describe env2, which requires a different NumPy version (1.21.0) but runs the same main1.py file. If you think of main1.py as a pytest file and env1 and env2 as two environments against which you want to test, this will make sense.

Running Codes

To run main1.py from the above two environments, we type

tox -e env1

and

tox -e env2

respectively. When these commands are run for the first time, tox creates env1 and env2 environments within a hidden directory .tox. If you list .tox directory, your will be able to see them. Because of these installations, the first time when we run the above commands they may take a bit longer to run. But you will notice that after the first time, those commands run faster because it is no longer needed to install the packages (they are already installed first time).

Tox creates environment directories env1 and env2 within .tox directory

Now we will create one more environment called env3. It however, makes use of existing environment directory env1 and it has the same dependencies as env1. This means this new environment will use files within .tox/env1 to run the commands. We list two commands within env3 to run main1.py and main2.py. So when we run tox -e env3, tox will utilize the files within .tox/env1 to run main1.py and main2.py. You will see the NumPy version 1.20.0 printed by both main1.py and main2.py, and “hello” printed to the screen by main2.py.

main2.py
envdir for env3 is set as env1 (see line 19 in tox.ini file), so no env3 directory is created within .tox

Default Runs

If you would like to run a subset of environments by default with “tox” command (no environment is specified with “-e”), you can list them under the section [tox]. For instance if you specify

envlist = env2, env3

within [tox] section of tox.ini file, Tox will run env2 and env3 by default. This means running main1.py with NumPy 1.21.0 (env2) and running main1.py and main2.py with NumPy 1.20.0 (env3).

Conclusions

As you saw above, with Tox it is very easy to set up different python environments and to run different testing codes. It is very flexible and allows developers to focus on developing their product instead of spending a lot of time on setting up environment

--

--

Praveen NG

A data science professional with a research background.