Enhance Your Testing Efficiency with Tox and Pytest
Written on
Chapter 1: Introduction to Continuous Integration
When collaborating on a shared codebase, continuous integration (CI) significantly enhances productivity. CI is the practice of regularly merging code changes from all developers into a shared repository, often multiple times daily. This practice proves beneficial even for solo projects.
Most CI tools operate on a server; however, Tox is an automation tool that functions similarly to CI tools and can be executed both locally and on servers alongside other CI tools.
Section 1.1: What is Tox?
Tox is a command-line utility designed to execute your entire suite of tests across various environments. After testing in all specified environments, Tox provides a summary of the results.
Section 1.2: Configuring Your Tox Environment
To get started, you will need to set up the tox.ini file. By using the envlist, you can define which Python versions to employ. However, this could lead to issues on local machines if not all specified versions are installed. To address this, you can use the skip_missing_interpreters option, ensuring that tests only run for the Python versions that are available locally. This becomes particularly useful in CI environments where all specified versions will be utilized.
In the [deps] section, you list the packages required for your project, while the [commands] section outlines the commands to execute. Interestingly, you can also set a minimum coverage percentage that must be achieved for the tests to pass. This is crucial in CI, as you want to avoid merging code that doesn't meet a certain coverage standard.
[tox]
envlist= py310, py311, py312
skip_missing_interpreters = True
[testenv]
deps =
pytest
pytest-cov
commands = pytest --cov --cov-fail-under=80
If you'd like to test this on your machine, simply execute the Tox command. The output should resemble the following structure:
Chapter 2: Integrating Tox with CI
We can choose when to run our tests; in this case, we opt to run them during pull requests. Within the strategy.matrix, we define variables that customize the test environment.
The subsequent process is straightforward: we use predefined actions to install the Tox package and run Tox using the configurations set in tox.ini.
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python: ["3.10", "3.11", "3.12"]
steps:
uses: actions/checkout@v4
name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
name: Install Tox and any other packages
run: pip install tox
name: Run Tox
run: tox
This video, titled "Automated Testing in Python with pytest, tox, and GitHub Actions," provides an in-depth look at how to automate your testing process using Tox and Pytest, enhancing your workflow.
The second video, "Automating Build, Test and Release Workflows with tox," elaborates on how to integrate Tox into your build and release cycles effectively.
You can view the complete code in our GitHub repository. If you found this information helpful and want to join our expanding community, please follow us. Your feedback is valuable, so feel free to share your thoughts!
Thank you for being part of the In Plain English community! Before you leave, be sure to follow us on X, LinkedIn, YouTube, Discord, and subscribe to our newsletter. Explore more content at Stackademic, CoFeed, and Venture.