search
HomeBackend DevelopmentPython TutorialPython task automation tool tox tutorial

Python task automation tool tox tutorial

Apr 19, 2023 pm 05:49 PM
pythonAutomation toolstox

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

Python task automation tool tox tutorial

#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:

  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 that it is configured like this (https:// github.com/tox-dev/tox/blob/master/tox.ini):

Python task automation tool tox tutorial

Python task automation tool tox tutorial

##Every [xxx ] and the content below it form a section, with blank lines between each section.


[tox]The following are global configuration items. The envlist field defines the environment in which tox operates. [xxx] The following are the configuration items of the xxx virtual environment. [xxx:yyy] inherits the configuration of xxx, and its own configuration items have a higher priority.


For each virtual environment, there are many configuration items available. For example, commonly used ones are: description (description information), basepython (Python interpreter version), deps (environment dependencies), commands (commands) statement) and so on.


tox also supports variable substitution, which provides some built-in basic variables (global or for virtual environments): {toxinidir}, {homedir}, {envname}, {envdir}, etc. wait.


In addition to basic variable substitution, it also supports these advanced usages:


    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

Pass command line parameters: {posargs:DEFAULTS}. When there are no command line parameters, the DEFAULTS value is used. Usage: tox arg1 arg2 to pass two parameters, or tox -- --opt1 arg1 to pass "--opt1 arg1" as a whole.


    Values ​​passed between chapters: {[sectionname]valuename}, the contents of different chapters can be passed and used.

  • Interactive console injection: {tty:ON_VALUE:OFF_VALUE}, when the interactive shell console is opened, the first value is used, otherwise the second one is used. This is an example when pytest uses "--pdb".

In addition to being used for variable substitution, the curly braces "{}" can also be used as the value of the "OR relationship" judgment. Look directly at the following example:

[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.


About tox’s configuration items, usage conditions, meaning, advanced usage, etc., you can view it in the official documentation: https://tox.readthedocs.io/en/latest/config .html


3. Plug-in of tox


In addition to its strong configurability, tox also has strong scalability and is pluggable. (pluggable), around which an extremely rich plug-in ecosystem has emerged.


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.

Python task automation tool tox tutorial

tox has opened a lot of API interfaces to facilitate others to customize and develop plug-ins.

Python task automation tool tox tutorial

4. The workflow of tox

Let’s take a look at how tox works:

Python task automation tool tox tutorial

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.

Python task automation tool tox tutorial

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!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
What data types can be stored in a Python array?What data types can be stored in a Python array?Apr 27, 2025 am 12:11 AM

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

What happens if you try to store a value of the wrong data type in a Python array?What happens if you try to store a value of the wrong data type in a Python array?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Which is part of the Python standard library: lists or arrays?Which is part of the Python standard library: lists or arrays?Apr 27, 2025 am 12:03 AM

Pythonlistsarepartofthestandardlibrary,whilearraysarenot.Listsarebuilt-in,versatile,andusedforstoringcollections,whereasarraysareprovidedbythearraymoduleandlesscommonlyusedduetolimitedfunctionality.

What should you check if the script executes with the wrong Python version?What should you check if the script executes with the wrong Python version?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

What are some common operations that can be performed on Python arrays?What are some common operations that can be performed on Python arrays?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

In what types of applications are NumPy arrays commonly used?In what types of applications are NumPy arrays commonly used?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

When would you choose to use an array over a list in Python?When would you choose to use an array over a list in Python?Apr 26, 2025 am 12:12 AM

Useanarray.arrayoveralistinPythonwhendealingwithhomogeneousdata,performance-criticalcode,orinterfacingwithCcode.1)HomogeneousData:Arrayssavememorywithtypedelements.2)Performance-CriticalCode:Arraysofferbetterperformancefornumericaloperations.3)Interf

Are all list operations supported by arrays, and vice versa? Why or why not?Are all list operations supported by arrays, and vice versa? Why or why not?Apr 26, 2025 am 12:05 AM

No,notalllistoperationsaresupportedbyarrays,andviceversa.1)Arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,whichimpactsperformance.2)Listsdonotguaranteeconstanttimecomplexityfordirectaccesslikearraysdo.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft