Python task automation tool tox tutorial
- Command line driven CI frontend and development task automation tool
- Command line driven CI frontend and development task automation tool
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.
1. What can tox do?
Segmented uses include:
- Create a development environment
- Run static code analysis and testing tools
- Automated building packages
- Run tests against tox-built packages
- Check whether the package can be successfully installed in different Python versions/interpreters
- Unify continuous integration (CI) and command line based testing
- Create and deploy project documentation
- Publish packages to PyPI or any other Platform
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
#2. How to configure tox?
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:
- pyproject.toml
- tox.ini
- setup.cfg
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):
- Get the environment variable of the operating system: {env:KEY}, the effect is the same in os.environ['KEY']. Can be changed to: {env:KEY:DEFAULTVALUE}, the default value is used when the environment variable cannot be obtained; {env:KEY:{env:DEFAULT_OF_KEY}}, to achieve the value effect of if-else
-
- Values passed between chapters: {[sectionname]valuename}, the contents of different chapters can be passed and used.
[tox] envlist = {py27,py36}-django{15,16}
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.
4. The workflow of tox
Let’s take a look at how tox works:
The main links in the workflow are:
- Configuration (from configuration): load the configuration file (such as tox.ini), parse the command Line parameters, read system environment variables, etc.
- Packaging: Optional, for projects with setup.py files, 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 "deps" in the configuration item, and then execute the configured commands (commands)
- Report: Summarize the running results of all virtual environments and list them
5. Summary
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
