如何创建 tox.ini 个变量

How to create tox.ini variables

有没有办法在 tox.ini 中设置任意变量?

一个示例是可以以多种方式使用的项目名称。对于一个相当复杂的 tox.ini,我发现自己复制并粘贴到我应该只需要在顶部设置一个变量的地方。

举个例子供参考tox.ini:

[tox]
envlist = clean, py{27,35,py}, license, style
skipsdist = True
skip_missing_interpreters = True
sitepackages = False

[testenv:clean]
deps = coverage
skip_install = true
commands =
    hash -r
    find {toxinidir} -name '*.pyc' -delete
    find {toxinidir} -name '__pycache__' -delete
    coverage erase
    rm -Rf {toxinidir}/docs/_build {toxinidir}/docs/coverage {toxinidir}/docs/reports

[testenv]
passenv = *
whitelist_externals = *
install_command = {envpython} -m pip install -q --process-dependency-links {opts} {packages}
envdir = {env:WORKON_HOME}/tox-<project_name>/{envname}
sitepackages = False
recreate = True
commands =
    # hash -r
    py{27,35,py}: {envpython} -m pytest --cov-append --cov=<project_name> --html=docs/reports/{envname}-report.html {posargs}
    smoke: {envpython} -m pytest -m smoke --cov-append --cov=<project_name> --html=docs/reports/{envname}-report.html {posargs}
    unit: {envpython} -m pytest -m unit --cov-append --cov=<project_name> --html=docs/reports/{envname}-report.html {posargs}
    integration: {envpython} -m pytest -m integration --long-running --cov-append --cov=<project_name> --html=docs/reports/{envname}-report.html {posargs}
    requirements: {envpython} -m pytest --cov-append --cov=<project_name> --html=docs/reports/{envname}-report.html {posargs}
    license: {envpython} -m pytest -m license --license --cov-append --html=docs/reports/{envname}-report.html {posargs}
    py{27,35,py}-smoke: {envpython} -m pytest -m smoke --cov-append --cov=<project_name> --html=docs/reports/{envname}-report.html {posargs}
    py{27,35,py}-unit: {envpython} -m pytest -m unit --cov-append --cov=<project_name> --html=docs/reports/{envname}-report.html {posargs}
    py{27,35,py}-integration: {envpython} -m pytest -m integration --long-running --cov-append --cov=<project_name> --html=docs/reports/{envname}-report.html {posargs}
    py{27,35,py}-requirements: {envpython} -m pytest --cov-append --cov=<project_name> --html=docs/reports/{envname}-report.html {posargs}
    py{27,35,py}-license: {envpython} -m pytest -m license --cov-append --html=docs/reports/{envname}-report.html {posargs}
deps =
    --editable=file:///{toxinidir}[tests]
    --editable=file:///{toxinidir}
    py{27,35,py}-requirements: -r{toxinidir}/requirements.txt


[testenv:coverage-report]
deps = coverage
skip_install = true
whitelist_externals = *
commands =
    hash -r
    coverage combine
    coverage report -m

[testenv:docs]
sitepackages = False
whitelist_externals = *
recreate = True
deps = --editable=file:///{toxinidir}[docs]
commands =
    hash -r
    coverage html --directory=docs/coverage
    coverage html
    {envpython} setup.py build_sphinx

[testenv:style]
whitelist_externals = *
sitepackages = False
recreate = True
commands =
    py.test -q --flake8 <project_name>/ --html=docs/reports/{envname}-report.html {posargs}

[testenv:vagrant]
passenv = *
whitelist_externals = *
sitepackages = False
recreate = False
skip_install = true
changedir = {toxinidir}/provision/vagrant
commands =
    hash -r
    vagrant destroy --force
    vagrant up

您可以使用 {[section]varname} 引用 tox.ini 中的变量。

这是一个如何使用变量的示例 ignore_list:

[main]
ignore_list = "E201,E202,E203,E221,E231,E241,E265,E266,E272,E402,W293,W391"

[testenv]
commands =
    pep8 \
    --max-line-length=120 \
    --ignore={[main]ignore_list}