4

Is there a way to set arbitrary variables within tox.ini?

An example would be a project name that might be used in a variety of ways. With a rather complex tox.ini, I find myself copy and pasting all over where I should just need to set a variable at the top.

For reference, an example 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
Brian Bruggeman
  • 5,008
  • 2
  • 36
  • 55

1 Answers1

9

You can refer to variables within tox.ini with {[section]varname}.

This is an example of how you can work with the variable 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}      
imolit
  • 7,743
  • 3
  • 25
  • 29
pll
  • 116
  • 1
  • 5
  • Awesome! You made my day. Thanks! – Brian Bruggeman Feb 17 '17 at 17:00
  • Wow such a hidden feature. Finally found it under [Substitution for values from other sections](https://tox.readthedocs.io/en/latest/config.html#substitution-for-values-from-other-sections). – Janos Mar 11 '20 at 00:02