Skip to content

๐Ÿงช Python Testing with Tox

A comprehensive guide to testing Python packages across multiple environments

Tox Config Reference

INI
[tox]
envlist = py{311,312},docs-py{311,312}
isolated_build = True
packagedir = .
skipsdist = True

[testenv]
platform =
    linux: linux
    macos: darwin
    windows: win32
description = Run tests with pytest
# Use test dependencies from pyproject.toml
deps =
    pytest
    pytest-cov
    -e .[test]
package = wheel

[testenv:py311]
basepython = python3.11

[testenv:py312]
basepython = python3.12

[testenv:docs-py311]
description = Build documentation using MkDocs with Python 3.11
basepython = {[testenv:py311]basepython}
# Use dependencies from pyproject.toml docs extra
deps =
    -e .[dev]
commands =
    # First run a build without strict mode to see warnings
    mkdocs build -v
    # Then run strict mode if needed
    mkdocs build -v --strict


[testenv:docs-py312]
description = Build documentation using MkDocs with Python 3.12
basepython = {[testenv:py312]basepython}
# Reuse deps from docs-py311
deps = {[testenv:docs-py311]deps}
commands = {[testenv:docs-py311]commands}

๐Ÿš€ Quick Start

Installation

Bash
pip install tox

Basic Usage

Bash
# Run tests in all environments defined in tox.ini
tox

# Run tests in a specific environment
tox -e py311

๐Ÿ“š Documentation Testing

Ensure your docs build correctly before deployment

๐Ÿ” Running Documentation Checks

Use these commands to test your documentation builds:

Bash
# Build docs with Python 3.11 (verbose mode with colored output)
tox -e docs-py311 -v --colored yes

# Build docs with Python 3.12 (verbose mode with colored output)
tox -e docs-py12 -v --colored yes
๐Ÿท๏ธ Flag Description
-e Specifies the environment to run
-v Verbose output mode
--colored yes Enables colored terminal output

๐Ÿ”„ Testing Flow

1๏ธโƒฃ Setup

Before running tox, ensure you have the following installed:

  • Python versions 3.10, 3.11, and 3.12 (installed and available in your PATH)
  • tox (pip install tox)

2๏ธโƒฃ Understanding Our Configuration

Our tox.ini file is configured to:

  • Test against Python 3.10, 3.11, and 3.12
  • Install all necessary dependencies
  • Build the documentation to verify it renders correctly
  • Run any tests (when uncommented)

3๏ธโƒฃ Running Tests

Basic Execution

To run tests across all configured Python versions:

Bash
tox

Testing Specific Python Version

To test against a specific Python version:

Bash
tox -e py310  # Test with Python 3.10
tox -e py311  # Test with Python 3.11
tox -e py312  # Test with Python 3.12

Documentation Testing Only

To verify only the documentation builds:

Bash
tox -e docs

4๏ธโƒฃ Interpreting Results

  • Tox will create separate virtual environments for each Python version
  • For each environment, tox will:
    • Install dependencies
    • Run the configured commands
    • Report success or failure
  • A summary is displayed after all tests complete

๐Ÿ”ง Common Options

Option Description Example
-e ENV Run only specified environment tox -e py311,py312
-r Recreate virtual environments tox -r
-v Verbose mode tox -v
--parallel Run environments in parallel tox --parallel
--no-deps Skip dependency installation tox --no-deps

๐Ÿ› ๏ธ Troubleshooting

Issue Solution
Environment creation fails Run tox -r to recreate
Missing dependencies Check pyproject.toml extras
PyPI issues Try --offline mode
Failed tests Check .tox/*/log/*.log files

โš™๏ธ Tox Commands Reference

Command Description
tox Run tests in all environments
tox -e ENV Run tests in specific environment (e.g.,tox -e py311)
tox -r Recreate the test environments
tox -l List all available test environments
tox -p Run tests in parallel
tox --help Show help information

๐Ÿ”— Resources

Official Documentation

Additional Learning Resources

Community Support

๐Ÿ”„ Integration with CI/CD

Tox works seamlessly with continuous integration systems. Example configuration for GitHub Actions:

YAML
---
name: Python Tests

'on':
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.10, 3.11, 3.12]

    steps:
      - uses: actions/checkout@v3
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install tox tox-gh-actions
      - name: Test with tox
        run: tox

๐Ÿšซ Common Issues and Solutions

  • Missing Python version: Ensure all Python versions are installed and in PATH
  • Dependency conflicts: Check pyproject.toml for compatibility with all Python versions
  • Environment reuse: Use tox -r to recreate environments if you suspect cached issues
  • Slow test execution: Use tox -p for parallel execution of test environments

๐Ÿ“Š Best Practices

  • Keep tox environments focused on specific tasks
  • Use factors to avoid duplicating configurations
  • Set up CI/CD integration for automated testing
  • Maintain a comprehensive test suite to ensure package quality
  • Document your tox configuration for contributors

๐Ÿ Conclusion

Tox provides a powerful way to ensure your code works across different Python environments. By automating the testing process, we can maintain compatibility and catch issues early in the development cycle.

For any questions or issues with the tox configuration, please contact the project maintainers.