Home > Article > Backend Development > Python task automation tool tox tutorial
tox’s project address is: https://github.com/tox-dev/tox
Its core function is to support the creation of an isolated Python environment, in which it can be installed Different versions of Python interpreters and various dependent libraries make it easier for developers to do automated testing, packaging, continuous integration, etc.
Simply put, tox is a command line tool for managing test virtual environments. It has been around for many years and is widely used by developers. For example, the famous cloud computing platform OpenStack also uses it as one of the most basic testing tools.
Segmented uses include:
tox official documentation lists more than 40 examples of usage scenarios. The detailed list can be viewed at: https://tox.readthedocs.io/en/latest/examples. html
About its usage: Use pip install tox to install, use tox to run all test environments, and tox -e envname to run the specified environment. There are also many command line parameters, which can be viewed through tox -h.
The behavior of tox is controlled by its configuration file. Currently it supports 3 configuration files:
Taking the tox project’s own tox.ini configuration content as an example, you can see that it is configured like this (https:// github.com/tox-dev/tox/blob/master/tox.ini):
[tox] envlist = {py27,py36}-django{15,16}{py27,py36}-django{15,16} has 2 values in each of the 2 sets of curly brackets. They can actually be combined into 4 environments: py27-django15, py27-django16, py36-django15, py36-django16.
Using pip search tox, you can see a large number of libraries starting with "tox-", which are all plug-in packages of tox. Among them are well-known names such as setuptools, pipenv, conda, travis, pytest, and docker.
tox has opened a lot of API interfaces to facilitate others to customize and develop plug-ins.
Let’s take a look at how tox works:
The main links in the workflow are:
tox itself is positioned as a testing tool, which tries to make Python testing becomes automated, standardized and streamlined. But unlike testing frameworks such as unittest and pytest, it works beyond the code level and is a project-level tool. Therefore, it needs to be combined with these testing frameworks, or handle multiple automated tasks at the same time (such as running pep8, testing code coverage, generating documentation, etc.), so that it can better exert its value.
One of its major features is the creation/management of virtual environments, but this is only used to facilitate testing, so compared with other tools that can manage virtual environments, such as Virtualenvwrapper, conda, pipenv, poetry, it has shortcomings in some aspects.
tox also has strong configurability and rich plug-in support, which gives it great possibility and freedom in application. Therefore, many loyal developers are still using it. For example, the author of the series of articles I just translated is one of its maintainers.
Finally, I need to add that tox uses configuration files as drivers, but the configuration files are still quite cumbersome, so someone developed a nox that is similar to tox. Use Python files for configuration. This project is also very popular, attracting many projects to invest under its umbrella, such as pipx, urllib3, Salt, etc. If you are interested in this project, please check out: https://nox.thea.codes/en/stable.
The above is the detailed content of Python task automation tool tox tutorial. For more information, please follow other related articles on the PHP Chinese website!