Python tools to automate testing

As you know, in any development process there are quite a few repetitive tasks. In this article, we’ll focus on the tasks that can be automated in Python to streamline the process and avoid these repetitions. Let’s take a look at three Python tools.


tox Logo - Python Tools

tox is a project intended to automate and standardize testing in Python. It works as part of a larger vision that aims to ease the packaging, testing and release process of Python software.  

Let’s assume that we want to test our client’s Python project and that their requirements.txt is complete and correct. To do this, we would usually have to follow the 5 steps listed on the left side of the table below. However, with tox, this process can be reduced to just 1 step.

Without tox With tox
  1. Create a virtualenv
  2. Activate the virtualenv
  3. Install pytest
  4. Install requirements.txt
  5. Run pytest
  1. Run tox

Moreover, this process must be repeated for each version of dependencies that we want to test. As tox makes the process trivial, it allows us to test multiple Python environments. Imagine that you are dealing with Python 2 and Python 3 and you are testing different Flask versions, like 0.12 and 1.0. This is what it would look like:

# content of: tox.ini , put in same dir as
# this line indicates what virtual environments will be created.
envlist = py27,py36

# install testing framework

# ... or install anything else you might need here

deps = pytest
Flask012: flask=0.12.4
Flask102: flask=1.0.2
# run the tests

# ... or run any other command line tool you need to run here

commands = pytest

This configuration does four pytests and it multiplies the configuration, so it’s testing:

Py27 and Flask=0.12.4

Py27 and Flask=1.0.2

Py36 and Flask=0.12.4

Py36 and Flask=1.0.2

This is really useful since it means we don’t have to spend precious time performing the five initial steps for each of these four configurations. For more detailed information, you should visit tox’s official website.

Beyond tox…

Nox Logo - Python tools

Similarly to tox, Nox is a command-line tool that gives you the possibility to automate testing in multiple Python environments. However, unlike tox, Nox actually uses a standard Python file for configuration.

This means you can configure Nox through a file within your project’s directory. Let’s take a look at a noxfile that runs lint and some tests:

Python as configuration is just like magic. This is a really freeing feature as anyone who knows how to program in Python already knows 99% of how to use Nox. All you need to learn is how to use a very small API. 

For more detailed information, you should visit Nox’s official website.

Beyond Nox…

Three Python tools to automate testing

Is Nox still not good enough for you? Well, if you need to take it one step further by using Python to do tasks in a non-Python project, you should definitely look into Invoke.

Invoke is a Python task execution tool and library that draws inspiration from several sources to provide a powerful and clean feature set. This tool uses the concept of tasks and makes considerably fewer assumptions than others, letting you do even more. 

Here’s an example:

As you can see, there’s an automated process to deploy our project. This task would normally take five commands but using Invoke narrows it down to one. For more detailed information, you should visit Invoke’s official website.

Final thoughts

If you or someone on your team see themselves using commands in a terminal every day, and sometimes even several times in one day, you definitely have plenty of reasons to automate some of that work. These Python tools can help you make your day-to-day work easier and more enjoyable, as well as take care of yourself and your team. They’re definitely worth a try.