Get startedGet started for free

Testing your package with different environments

1. Testing your package with different environments

Great work on writing those tests for your package. This will make it easy to check your package is working properly as you make changes.

2. Testing multiple versions of Python

But if you are trying to write a package which will work with multiple versions of Python, then you will need to test each version separately. This means you will need to install the versions of Python you would like to support, install your package and all of its dependencies within each Python version and run pytest.

3. Testing multiple versions of Python

An easier way to test multiple Python versions, and the most common way, is to use a tool called tox.

4. What is tox?

Tox is a tool specifically built to run package tests with multiple versions of Python. You can run tox from the terminal, just like pytest.

5. Configure tox

In order to use tox, you need to create a configuration file telling tox what to run in each environment and also which Python versions to use. The configuration file must be named tox-dot-ini and needs to be placed in the top level of the package, alongside the setup-dot-py file.

6. Configure tox

Inside the file you need to create the tox heading like this. You will then specify the versions of Python you want to test using the envlist parameter. Here we are testing python 2.7, 3.5, 3.6 and 3.7. You need to have these versions of Python already installed on your computer. Tox won't install new python versions. Using each of these versions of Python, tox will install your package and its dependencies. Under the testenv heading you need to tell tox what you want it to do once it has installed your package. You use the commands parameter to tell tox to run pytest. However, pytest isn't installed by your package dependencies, so you need to specify pytest as a tox dependency. You could actually add any commands you like to the commands list and tox will run them all. These need to be shell commands, which you could run from the terminal.

7. Running tox

To run tox, all you need to do is navigate to the top of your package and run the tox command.

8. tox output

For each version of Python you are testing you should see some output like this. This first section just tells you that tox has created a new Python 2.7 environment and installed the package and all the dependencies.

9. tox output

Next it starts running your commands. Here it runs pytest and shows the results from this. It repeats this for each version of Python you added in the configuration file.

10. tox output

and finally it shows you a summary.

11. tox output

If there was an error using one of the Python versions you would see output like this, and could use it to track down the cause of this error.

12. Let's practice!

Alright, time to test your package with multiple versions of Python. Let's practice!