程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

On the basic usage of Python task automation tool tox

編輯:Python

Catalog

Talking about Python Task automation tools Tox

introduction :

brief introduction :

Basic usage :

The configuration file :

tox workflow :

install :

Use :

Summary :

Talking about Python Task automation tools Tox introduction :

Recently collecting github Which contains test samples Python project , And try to docker Run through these projects in the environment , It is found that the main test frameworks used in these projects are : unittest, pytest ,nosetest. Others use automated tools Tox, So I have a brief understanding of .

brief introduction :

Command line driven CI frontend and development task automation tool
Command line driven CI Front end and development task automation tools

tox The project address of is :https://github.com/tox-dev/tox

Its core role is to support the creation of isolation Python Environmental Science , You can install different versions of Python Interpreter and various dependency libraries , This is convenient for developers to do automated testing 、 pack 、 Continuous integration and so on .

Simply speaking ,tox Is a command-line tool for managing test virtual environments . It has existed for many years and is widely used by developers , for example , Famous cloud computing platform OpenStack It's also used , As one of the most basic testing tools .

Basic usage :

install

pip install tox

Place basic information about the project and the test environment in which you want the project to run in a file that should be next to the file :tox.inisetup.py

# content of: tox.ini , put in same dir as setup.py[tox]envlist = py27,py36[testenv]# install testing framework# ... or install anything else you might need heredeps = pytest# run the tests# ... or run any other command line tool you need to run herecommands = pytest

To package 、 Installation and test items , You can now type... At the command prompt :

tox The configuration file :

tox The behavior of is controlled by its configuration file , Currently it supports 3 Configuration files :

pyproject.toml

tox.ini

setup.cfg

We use **python-project-wizard** Project as an example , Take a look at what the developers wrote tox The configuration file .

pyproject.toml

[tool][tool.poetry]name = "ppw"version = "1.1.1"description = "A Wizard to create a skeleton python project with up-to-date technology"license = "BSD-3-Clause"authors = ["Aaron Yang <[email protected]>"]readme = "README.md"repository = "https://github.com/zillionare/cookiecutter-pypackage"documentation = "https://zillionare.github.io/cookiecutter-pypackage/"keywords = ['cookiecutter', 'template', 'package']packages = [ {include = "ppw"}]include = [ '{{cookiecutter.project_slug}}/**/*', 'cookiecutter.json', 'hooks/*'][tool.poetry.dependencies]python = ">=3.7,<4.0"cookiecutter = "1.7.2"pytest = {version = "^5.4.3", optional=true}pytest-cookies = {version = "^0.5.1", optional=true}pyyaml = {version="^5.3.1",optional=true}mkdocs = {version="^1.1.2",optional=true}mkdocs-material = {version="^6.1.7",optional=true}mkdocs-material-extensions = {version="^1.0.1",optional=true}pytest-cov = {version="^2.10.1",optional=true}tox = {version = "^3.20.1", optional=true}mkdocs-include-markdown-plugin = {version = "^2.8.0", optional=true}fire = {version="^0.4.0", optional=true}pre-commit = {version="^2.18.1",optional=true}[tool.poetry.extras]dev = [ "pytest", "pytest-cookies", "pyyaml", "mkdocs", "mkdocs-material", "mkdocs-material-extensions", "pytest-cov", "tox", "mkdocs-include-markdown-plugin", "fire"][tool.black]line-length = 88include = '\.pyi?$'exclude = '''/( \.eggs | \.git | \.hg | \.mypy_cache | \.tox | \.venv | _build | buck-out | build | dist)/'''[build-system]requires = ["poetry-core>=1.0.0"]build-backend = "poetry.core.masonry.api"[tool.poetry.scripts]ppw = 'ppw.cli:main'

INI(.ini) File is a very primitive basic form , But each family has its own usage , And it can only solve one layer of nesting at most . Only suitable for very, very simple configuration files , Once you need two layers of nesting , Or an array is required , I can't do it .

finally ,TOML(.toml) Born in the sky . It completely abandons the underlying principle of parentheses or indentation , Instead, an explicit key name chain is used .

tox.ini

[tox]envlist = py37,py38,py39,py310, docsisolated_build = True[gh-actions]python = 3.7: py37 3.8: py38, docs 3.9: py39 3.10: py310[testenv:docs]basepython=pythonallowlist_externals = mkdocscommands= mkdocs build[testenv]extras = devsetenv = PYTHONPATH = {toxinidir}commands = pytest -s --cov-report=term-missing tests

Every [xxx] And the contents below make up a chapter (section), Use blank lines between each chapter .

[tox] Here are the global configuration items

[xxx:yyy] Inherit xxx Configuration of , At the same time, the priority of its own configuration items is higher .

For each virtual environment , There are many configuration items available , For example, common ones are :description( Description information )、basepython(Python Interpreter version )、deps( Environmental dependencies )、commands( Command statement ) wait .

tox workflow :

To configure ( from figuration): Load profile ( Such as tox.ini), Parsing command line arguments , Read system environment variables, etc

pack (packaging): Optional , For with setup.py The project of the document , You can generate its source distribution in this step

Creating a virtual environment : By default virtualenv To create a virtual environment , And according to “deps” Install the required dependencies , Then execute the configured command (commands)

The report (report): Summarize the running results of all virtual environments and list them Install and use :

install :

Use tox-quickstart Quickly generate tox.ini, You can also write according to the template tox.ini file

Use :

After configuring the file , Sure tox Direct operation , It's fine too tox -e test_api

If you run in another directory , You need to tox -c /test/tox.ini -c Specifies the configuration file

Summary :

tox Self positioning is a testing tool , It tries to make Python Testing becomes automated 、 Standardization and process . But follow unittest and pytest These test frameworks are different , It's about things outside the code level , It's a project level tool . therefore , It needs to be combined with these test frameworks , Or handle multiple automation tasks at the same time ( Like running pep8、 Test code coverage 、 Generate documents and so on ), In this way, we can better play its value .

One of its features is the creation of / Manage virtual environments , But it's just a means to facilitate testing , So compared with other tools that can manage virtual environment , Such as Virtualenvwrapper、conda、pipenv、poetry, It has its shortcomings in some ways .

This is about talking about Python Task automation tools Tox This is the end of the article , More about Python Task automation tools Tox Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved