Tox: Testing Python Across Multiple Versions

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction

When developing Python libraries or applications, ensuring compatibility across different interpreter versions is critical. This becomes especially important when publishing packages to PyPI, where users may run various Python versions. Manually installing Python 3.7, 3.8, 3.9, 3.10 and testing each one is time-consuming and tedious.

That's where tox comes in — a powerful test automation tool that runs your tests in isolated virtual environments with different Python versions. Tox automatically installs dependencies, creates clean environments, and guarantees reproducible test results.

What is Tox and Why Use It

Tox (short for "testing out of the box") is a command-line tool written in Python that automates testing across multiple environments. It creates isolated virtual environments, installs dependencies, and runs tests — letting you verify compatibility with different Python versions and dependency sets.

Key Benefits of Tox:

Environment Isolation — each test runs in its own virtual environment, eliminating dependency conflicts.

Automation — fully automated process for creating environments, installing dependencies, and running tests.

Multi-Version Testing — test simultaneously across different Python versions.

Reproducibility — guarantees consistent results across different environments.

CI/CD Integration — easy setup for automated testing in continuous integration systems.

Installation and Initial Setup

Installing Tox

You can install tox in several ways:

# Via pip
pip install tox

# Via conda
conda install -c conda-forge tox

# Via pipx (recommended)
pipx install tox

Creating a Basic Configuration File

The main configuration file for tox is tox.ini, which should be placed in your project's root directory:

[tox]
envlist = py38, py39, py310, py311

[testenv]
deps = 
    pytest
    pytest-cov
commands = 
    pytest tests/ --cov=src/

Project Structure

For tox to work correctly, we recommend the following project structure:

my_project/
├── src/
│   └── my_package/
│       ├── __init__.py
│       └── module.py
├── tests/
│   ├── __init__.py
│   └── test_module.py
├── tox.ini
├── setup.py (or pyproject.toml)
└── requirements.txt

Tox Basics

Basic Commands

Tox provides a simple command-line interface:

# Run all environments from envlist
tox

# Run a specific environment
tox -e py39

# Run multiple environments
tox -e py38,py39,lint

# Force recreate environments
tox -r

# Run with verbose output
tox -v

# Very verbose output (for debugging)
tox -vv

How Tox Works

The tox workflow consists of the following steps:

  1. Configuration Analysis — reads the tox.ini file and determines the list of environments
  2. Virtual Environment Creation — creates a virtual environment for each specified Python interpreter
  3. Package Installation — installs the package under test into each environment
  4. Dependency Installation — installs dependencies listed in the deps section
  5. Command Execution — runs the commands defined in the commands section

Recommendations