如何让 tox 测试 "named" 不同 python 版本的测试环境?

How to get tox to test "named" test environments with different python versions?

例如假设我在 tox.ini

中关注
[tox]
envlist = py27, py35, testenv2

[testenv]  
 # settings related to "default" testenv - includes deps, commands

[testenv:testenv2]  
 # settings related to testenv2 - includes deps, commands

现在,当我 运行 tox 命令时,它会调用带有 python 2.7 和 3.5 解释器的 testenv 命令,但 testenv2 命令仅在安装基础 python 时调用在机器上(在我的例子中是 2.7)。如何让 tox 也测试 "named"(非默认)测试环境,如 testenv2 以使用多个 python 版本进行测试?

明确列出所有环境:

[tox]
envlist = py27, py35, py27-testenv2, py35-testenv2

如果这还不够重构tox.ini:

[testenv]  
 # settings related to "default" testenv - includes deps, commands

[testenv2]  
 # settings related to testenv2 - includes deps, commands

[testenv:py27-testenv2]  
deps = {[testenv2]deps}
commands = {[testenv2]commands}

[testenv:py35-testenv2]  
deps = {[testenv2]deps}
commands = {[testenv2]commands}

第一个答案描述了两种可行的方法。为了完整起见:您还可以生成环境并使用条件设置 - 例如:

[tox]
skipsdist = True
envlist = py{27,35}-{test,lint}
[testenv]
skip_install = True
deps =
    test: pytest
    test: pytest-xprocess
    lint: flake8
    lint: black
commands =
    test: pytest -v
    lint: flake8
    lint: black .

会生成 (tox -a):

py27-test
py27-lint
py35-test
py35-lint

您可以使用任何因素(例如 py27 或 test)有条件地添加命令、deps 等

另见 docs

顺便说一句:要查看每个 testenv 究竟有哪些设置,您可以 运行 tox --showconfig.

我可以通过创建多个 "named" 测试环境来使用多个 python 版本测试 "named" 测试环境,每个版本对应不同的 python 版本我希望使用 basepython 选项对其进行测试,以指定要测试的测试环境的 python 版本。以下是有效的示例:

[tox]
envlist = py27, py35, testenv2_py27, testenv2_py35

[testenv]  
 # settings related to "default" testenv - includes deps, commands

[testenv:testenv2_py27]
basepython = python2.7 
 # settings related to testenv2 - includes deps, commands

[testenv:testenv2_py35]
basepython = python3.5  
 # settings related to testenv2 - includes deps, commands