panhandlefamily.com

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.

Tox command-line interface showcasing test 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:

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.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

The Kardashians' Business Model: A Lesson for Modern Entrepreneurs

Discover how the Kardashians' approach to fame offers a new perspective on financial success beyond traditional education.

Boost Your Productivity: 3 Easy Ways to Feel Accomplished

Discover simple methods to feel productive without actual work. Enhance your mood and motivation with these easy tasks.

Essential Career Guidance for Aspiring Software Engineers

Key tips for junior software engineers and students to navigate their careers effectively.

Exploring the Roots of Lazy Thinking and Its Consequences

An examination of the phenomenon of lazy thinking and its impact on beliefs and decision-making.

Unlocking Online Income: Four Secrets to Boost Your Earnings

Discover four effective strategies for making money online, from consultation calls to digital products, and transform your financial future.

Navigating the Journey of Leaving a Corporate Job: Key Insights

Discover essential lessons learned before leaving a corporate job for entrepreneurship, focusing on financial readiness and emotional resilience.

The Science Behind Why Snow Appears White

Explore the fascinating reasons why snow appears white despite being made of transparent water ice.

Nine Search Engines That Preceded Google’s Dominance

A nostalgic look at the search engines that once ruled the internet before Google took over.