In the Python packaging series I just translated, the author mentioned tox, an amazing testing tool, and he himself is one of the maintainers of tox. While the topic is relevant, this article will briefly introduce it in case you need to use it in your development projects.

Command line driven CI frontend and development task automation tool

Command line driven CI front end and development task automation tools

Tox’s project address is github.com/tox-dev/tox

At its core, it supports the creation of isolated Python environments where different versions of Python interpreters and dependent libraries can be installed to facilitate automated testing, packaging, continuous integration, and more.

In a nutshell, Tox is a command-line tool for managing test virtual environments. It has been around for years and is widely used by developers. For example, OpenStack, the well-known cloud computing platform, uses it as one of the most basic testing tools.

1. What can TOX do?

Subdivision uses include:

  • Creating a development environment
  • Run static code analysis and test tools
  • Automated build pack
  • Run tests against the tox-built software package
  • Check that the package can be installed successfully in different Python versions/interpreters
  • Unified Continuous integration (CI) and command-line based testing
  • Create and deploy project documentation
  • Publish the package to PyPI or any other platform

Tox official document lists the sample of more than 40 kinds of usage scenarios, a detailed list to view: tox. Readthedocs. IO/en/latest/e…

How to configure TOX?

About its usage: Use PIP Install tox to install, run all test environments with tox, and tox -e envname to run the specified environment. There are also quite a few command-line arguments, which you can view through tox-h.

Tox’s behavior is controlled by its profile, which currently supports three profiles:

  1. pyproject.toml
  2. tox.ini
  3. setup.cfg

Taking the tox project’s own tox. Ini configuration content as an example, you can see it configured like this (github.com/tox-dev/tox…) :

Each [XXX] and the content below it form a section, with blank lines separating each section.

Below [tox] are the global configuration items. The envList field defines the environment in which tox is to be operated. [XXX] The following is the configuration items of the XXX virtual environment. [XXX: YYy] inherits the configuration of XXX and its own configuration items have higher priorities.

For each virtual environment, there are many configuration items available, such as: Description (description information), Basepython (Python interpreter version), DEPS (environment dependencies), Commands (command statements), and so on.

Tox also supports variable substitution, which provides some built-in base variables (global or for virtual environments) : {toxinidir}, {homedir}, {envname}, {envdir}, and so on.

In addition to basic substitution, it supports these advanced uses:

  • Take the operating system environment variable: {env:KEY}, the effect is the same asos.environ['KEY']. {env:KEY:DEFAULTVALUE}. If no environment variable is available, use the DEFAULTVALUE. {env:KEY:{env:DEFAULTOFKEY}}, achieve if-else effect
  • Pass command line arguments: {posargs:DEFAULTS}, using DEFAULTS values when there are no command line arguments. Usage:tox arg1 arg2Pass two parameters, ortox -- --opt1 arg1Pass “– opt1 arg1” as a whole.
  • Value transfer between sections: {[sectionName] Valuename}. Contents of different sections can be transferred.
  • Interactive console injection: {tty:ONVALUE:OFFVALUE}, the first VALUE is used when the interactive shell console is turned on, and the second VALUE is used otherwise. Pytest is an example of this when using “– PDB”.

In addition to variable substitution, the curly braces “{}” can also be used to determine the value of “or relationship”. Look directly at the following example:

(tox) envlist = {py27, py36} - django 15 and 16th {}Copy the code

The two sets of {py27,py36}-django{15,16} each have two values inside curly braces. They can actually be combined to form four environments: py27-django15, py27-django16, py36-django15, and py36-django16.

What about tox configuration items and conditions of use, what meaning, advanced usage, and so on, can see in the official documentation: tox. Readthedocs. IO/en/latest/c…

3. Tox plug-in

In addition to its great configurability, Tox is also highly scalable, pluggable, and has spawned a very rich plug-in ecosystem around it.

Using PIP Search Tox, you can see a number of libraries beginning with “tox-” that are plug-in packages for Tox. Setuptools, Pipenv, Conda, Travis, PyTest, Docker, etc.

Tox opens up a lot of APIS, making it easy for others to customize plug-ins.

4. Tox workflow

Here’s how TOX works:

The main links in its workflow are as follows:

  • Configuration (from figuration) : loads configuration files (such as tox.ini), parses command line arguments, reads system environment variables, etc
  • Packaging: Optional, for projects with the setup.py file, you can generate its source distribution in this step
  • Create a virtual environment: Use virtualenv by default to create a virtual environment, install the required dependencies according to the “deps” in the configuration item, and then execute the configured commands
  • Report: Summarize the running results of all virtual environments and list them

5, summary

Tox itself is a testing tool that attempts to automate, standardize, and streamline Pytho testing. But unlike test frameworks like UnitTest and PyTest, it is a project-level tool that goes beyond the code level. Therefore, it needs to be integrated with these testing frameworks, or handle multiple automated tasks simultaneously (such as running PEP8, testing code coverage, generating documentation, etc.) to maximize its value.

One of its great features is creating/managing virtual environments, but this is just for testing purposes, so it falls short in some ways compared to other tools that can manage virtual environments such as Virtualenvwrapper, Conda, Pipenv, poetry.

Tox also has great configurability and extensive plug-in support, which gives it a lot of possibilities and freedom to use. As a result, many dedicated developers continue to use it. For example, the author of the series of articles I just translated is one of its maintainers.

Finally, tox is driven by a configuration file, but the configuration file is a bit cumbersome, so someone developed a similar version of nox that uses Python files to do the configuration. The project was also very popular and attracted many projects such as PIPX, URllib3, Salt, etc. Interested in this project, please check: nox. Thea. Codes/en/stable /

The public account “Python Cat”, this serial quality articles, cat philosophy series, Python advanced series, good books recommended series, technical writing, quality English recommended and translation, etc., welcome to pay attention to oh.