Python任务自动化工具Tox怎么使用

发布时间:2022-06-23 09:49:33 作者:iii
来源:亿速云 阅读:284

这篇文章主要讲解了“Python任务自动化工具Tox怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python任务自动化工具Tox怎么使用”吧!

浅谈Python任务自动化工具Tox

引言:

最近在搜集github上包含测试样例的Python项目,并试图在docker环境下跑通这些项目,发现这些项目主要使用的测试框架有 : unittest, pytest ,nosetest。还有一些用到了自动化工具Tox,所以简单了解了一下。

简介:

Command line driven CI frontend and development task automation tool
命令行驱动的 CI 前端和开发任务自动化工具

其核心作用是支持创建隔离的 Python 环境,在里面可以安装不同版本的 Python 解释器与各种依赖库,以此方便开发者做自动化测试、打包、持续集成等事情。

简单来说,tox 是一个管理测试虚拟环境的命令行工具。 它已存在多年且广被开发者们使用,例如,著名的云计算平台 OpenStack 也采用了它,作为最基础的测试工具之一。

基本用法:

安装

pip install tox

将有关项目和希望项目在其中运行的测试环境的基本信息放入应位于文件旁边的文件中:tox.ini setup.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 here
deps = pytest
# run the tests
# ... or run any other command line tool you need to run here
commands = pytest

若要打包、安装和测试项目,现在可以在命令提示符下键入:

tox

配置文件:

tox 的行为由其配置文件控制,当前它支持 3 种配置文件:

我们以**python-project-wizard**项目为例,看一下开发人员写的tox配置文件。

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 <aaron_yang@jieyu.ai>"]
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 = 88
include = '\.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'

tox.ini

[tox]
envlist = py37,py38,py39,py310, docs
isolated_build = True

[gh-actions]
python =
    3.7: py37
    3.8: py38, docs
    3.9: py39
    3.10: py310

[testenv:docs]
basepython=python
allowlist_externals = mkdocs
commands= mkdocs build

[testenv]
extras =
    dev

setenv =
    PYTHONPATH = {toxinidir}

commands = pytest -s --cov-report=term-missing tests

tox 的工作流程:

Python任务自动化工具Tox怎么使用

安装:

使用tox-quickstart快速生成tox.ini,也可以根据模板手写tox.ini文件

使用:

Python任务自动化工具Tox怎么使用

感谢各位的阅读,以上就是“Python任务自动化工具Tox怎么使用”的内容了,经过本文的学习后,相信大家对Python任务自动化工具Tox怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. Python任务自动化工具tox使用教程
  2. python怎么基于celery实现异步任务周期任务定时任务

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python tox

上一篇:C++算术运算符如何使用

下一篇:typescript中keyof与typeof操作符怎么使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》