【问题标题】:python tox: how to use a named env, with multiple versions?python tox:如何使用具有多个版本的命名环境?
【发布时间】:2020-03-19 12:45:07
【问题描述】:

我需要能够调用标记环境(由于 CICD 设置不仅调用 tox 而是调用 tox -e tag1,tag2,...),但我希望该环境使用多个 python 版本。我想要这样的东西:

[tox]
envlist = mytag

[testenv:mytag]
deps=
    pytest
    coverage
    pytest-cov
python_versions = py37, py38 // I KNOW THIS IS NOT RIGHT

commands=
    pytest ...

我目前知道如何做到这一点的唯一方法如下所示(调用tox -e mytag37,mytag38,但随着新版本的添加,这很难扩展)

[tox]
envlist = mytag37, mytag38

[testenv:mytag37]
basepython=python37 // see
deps=
    pytest
    coverage
    pytest-cov

commands=
    pytest ...


[testenv:mytag38]
basepython=python38 // see
deps=
    pytest
    coverage
    pytest-cov

commands=
    pytest ...

有没有更简洁的方法来实现这一点?

【问题讨论】:

  • 您可以生成带有 bash 扩展的环境,例如envlist = {py36,py37}-mytag 将为您生成两个环境,py36-mytagpy37-mytag。如果你想为单个 env 声明多个解释器,这是不可能的 - 每个 env 都使用自己的解释器。
  • 您是否有理由不使用默认的[testenv] 部分,我错过了什么吗?
  • 可以用-e something 之类的标签调用testenv 吗?另有说明,请阅读我帖子的第一句话。我认为something 必须是冒号后的标签,例如testenv:something
  • @hoefling 听起来你写的东西完全符合我的需要?基本上,能够从第一个问题中生成第二个代码块吗?如果您将其发布为答案,我将尝试接受!

标签: python-3.x pytest tox


【解决方案1】:

使用substitution:

[tox]
envlist = mytag37, mytag38

[mytag]
deps=
    pytest
    coverage
    pytest-cov
commands=
    pytest ...

[testenv:mytag37]
basepython=python37
deps={[mytag]deps}
commands={[mytag]commands}

[testenv:mytag38]
basepython=python38
deps={[mytag]deps}
commands={[mytag]commands}

【讨论】:

  • oooooo 不错的解决方案,我会试试这个
【解决方案2】:

不完全确定,也许我完全错过了你的问题的重点,但我相信你正在寻找的基本上是 tox 的默认工作模式,所以应该像下面这样够了:

[testenv]
deps =
    pytest
    coverage
    pytest-cov
commands =
    pytest ...

然后调用:

$ tox -e py36,py37,py38

【讨论】:

  • 如果您只调用toxtestenv 的默认行为是什么?什么python版本?
  • 你试过了吗?我相信,如果没有指定任何内容,那么 tox 只会退回到最初用于启动 tox 的 Python 解释器,它会创建一个名为 @ 的虚拟环境987654325@.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 2019-02-02
  • 2016-10-22
相关资源
最近更新 更多