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 .
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 .
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.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
To package 、 Installation and test items , You can now type... At the command prompt :
tox
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 = 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'
.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 ..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, 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
install :
Use tox-quickstart Quickly generate tox.ini, You can also write according to the template tox.ini file
Use :
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 .