如何减少 tox 文件中的重复

idj*_*jaw 6 python testing test-environments tox

目标:成功执行特定的 tox 命令,并让它为“仅”匹配的特定命令运行。

例子: tox -e py35-integration

tox应该只为 py35-integration 运行,包括默认或独立py35定义。

我尝试了两种不同的方法,据我所知,这是尝试做我想做的事情的两种方法。

  • 请注意,该flake8命令是为了轻松隔离不同的命令并向我指示正在运行的内容。这并不是我真正想要运行的命令的指示。

此外,ini 文件仅显示相关部分。

第一种方法

[tox]
envlist = {py27,py35}, {py27,py35}-integration

[testenv]
commands =
    py27: python -m testtools.run discover
    py35: python -m testtools.run discover
    py27-integration: flake8 {posargs}
    py35-integration: flake8 {posargs}
Run Code Online (Sandbox Code Playgroud)

使用这种方法,这里的理解是我希望在tox -e py27-integration不运行为py27命令定义的内容的情况下运行。这不是正在发生的事情。相反,它将同时运行py27py27-integration

第二种方法

[tox]
envlist = {py27,py35}, {py27,py35}-integration

[testenv]
commands =
    python -m testtools.run discover

[testenv:integration]
commands = 
    flake8 {posargs}
Run Code Online (Sandbox Code Playgroud)

现在,我在这里明确地隔离了一个“子”环境,它有自己的命令来运行“集成”。

但是,不幸的是,我遇到了与正在执行的所有匹配模式“py27”完全相同的行为。

我试图避免将 testenv 结构重复为:[testenv:py27-integration][testenv:py35-integration],它们包含完全相同的定义(目标是尽量减少重复)。

我很想知道是否有一种方法可以实现我正在尝试做的事情。

我不想冒险做一些类似p27-integration替代命名方案的事情,因为我们的 CI 管道有需要某些名称结构的模板,并且这些名称也是 tox 惯用的,py27例如被理解为安装 2.7 虚拟环境。

phd*_*phd 2

更新

[tox]
minversion = 3.15
envlist = {py27,py35}, {py27,py35}-integration

[testenv]
commands =
    python -m testtools.run discover

[testenv:py{27,35}-integration]
commands = 
    flake8 {posargs}
Run Code Online (Sandbox Code Playgroud)