如何在其他环境下运行tox -e myenv?

时间:2019-05-18 00:58:33

标签: python continuous-integration automated-tests pytest tox

在tox中是否有可能使tox -e hello触发hello环境并在全局envlist上运行?

运行tox对py2和py3正确运行。

运行tox -e hello仅针对py3运行。

[tox]
skipsdist = True
envlist = py{27,37}

[testenv]
deps =
    pytest
    !py27: mock
commands =
    {envpython} --version

; How to run this hello against the complete envlist?
[testenv:hello]
envlist = py{27,37}
commands =
    echo hello

这似乎在某种程度上是预期的行为,但是除了制作一堆tox文件之外,我无法弄清楚如何使用tox来解决问题:我想要tox具有不同的命令入口点。例如,仅针对py27运行单元测试,仅针对py{27,37}运行单元测试,仅针对py27-{libA,libB}运行集成测试,等等。

也许这只是一种反模式,您可以在单个环境中以这种方式使用tox -e,但这会破坏envlist吗?

那我只需要再创建一个tox.ini吗?考虑到其他所有关于毒药的灵活性和可重用性,这似乎很笨拙。我觉得我想念什么。

1 个答案:

答案 0 :(得分:2)

这里有两点要理解:

  1. envlist[tox]部分中的global setting。在[testenv:hello]中定义它无效
  2. envlist定义或生成env名称的列表。可以通过提供一个简单的逗号分隔列表来静态完成此操作,也可以像您一样,根据通过特殊语法{...}提供的因子组合生成列表。在两种情况下,结果都是一列环境名称,然后将在分开的virtualenvs / venvs中执行。

因此,要明确地说:envlist只是env名称的简单列表,仅此而已。这些名称(或其部分(也称为 factors ))在envs中的执行过程中正在发生。

如果您呼叫 -e,将执行所有这些环境。在您的示例中,您使用{<factorX>,<factorY>}语法的generating an envlist是基于命令行外壳程序所提供的相同原理。如果您在Tox配置中要求生成的环境,结果将是:

$ tox -a

py27-hello
py37-hello
hello

这可能已经表明您的配置有些问题,因为我认为您不希望使用不合格的hello环境。

因此,通过生成创建的envlist并使用hello语法静态提供[testenv:hello] testenv来进行的工作是将它们混合在一起,从而不会产生您想要的结果

因此,让我们调整您的tox.ini来摆脱不必要的静态定义testenv:hello。我们也摆脱了本地envlist键,因为它始终无效。它没有引发错误,因为明确允许envs中任意命名的键,并且有时是有用的。

[tox]
skipsdist = True
envlist = py{27,37}-hello

[testenv]
deps =
    pytest
    !py27: mock
# do not warn that echo is a command not in the venv
whitelist_externals = echo
commands =
    {envpython} --version
    hello: echo hello

为确保只运行hello命令,当环境具有hello因子时,可以使用<factor>[,<factor>]: <some command>指定如果环境名称包含某个{{3} }(以-分隔的名称部分) 正在生成环境名称列表,然后这些列表将用作正在执行的具体环境的信息。

如果我再次拨打tox -a更改的ini,我得到:

py27-hello
py37-hello

现在,我可以致电(例如) tox -e py27-hello甚至tox -e hello都将使用basepython并执行适合因子的所有操作。

要获取更多信息,您可以显示所有环境的完全解析的配置,看起来像这样:

$ tox --showconfig

[testenv:py27-hello]
  envdir          = /home/ob/do/play/.tox/py27-hello
  setenv          = SetenvDict: {'PYTHONHASHSEED': '1602746531', 'TOX_ENV_NAME': 'py27-hello', 'TOX_ENV_DIR': '/home/ob/do/play/.tox/py27-hello'}
  basepython      = python2.7
  description     = 
  envtmpdir       = /home/ob/do/play/.tox/py27-hello/tmp
  envlogdir       = /home/ob/do/play/.tox/py27-hello/log
  downloadcache   = None
  changedir       = /home/ob/do/play
  args_are_paths  = True
  skip_install    = False
  ignore_errors   = False
  recreate        = False
  passenv         = {'TOX_REPORTER_TIMESTAMP', 'TOX_WORK_DIR', 'TMPDIR', 'TOX_PARALLEL_ENV', 'LANGUAGE', 'LANG', 'PATH', 'LD_LIBRARY_PATH', 'PIP_INDEX_URL'}
  whitelist_externals = []
  platform        = .*
  sitepackages    = False
  alwayscopy      = False
  pip_pre         = False
  usedevelop      = False
  install_command = ['python', '-m', 'pip', 'install', '{opts}', '{packages}']
  list_dependencies_command = ['python', '-m', 'pip', 'freeze']
  deps            = [pytest]
  commands        = [['/home/ob/do/play/.tox/py27-hello/bin/python', '--version'], ['echo', 'hello']]
  commands_pre    = []
  commands_post   = []
  ignore_outcome  = False
  extras          = []
  depends         = ()
  parallel_show_output = False
[testenv:py37-hello]
  envdir          = /home/ob/do/play/.tox/py37-hello
  setenv          = SetenvDict: {'PYTHONHASHSEED': '1602746531', 'TOX_ENV_NAME': 'py37-hello', 'TOX_ENV_DIR': '/home/ob/do/play/.tox/py37-hello'}
  basepython      = python3.7
  description     = 
  envtmpdir       = /home/ob/do/play/.tox/py37-hello/tmp
  envlogdir       = /home/ob/do/play/.tox/py37-hello/log
  downloadcache   = None
  changedir       = /home/ob/do/play
  args_are_paths  = True
  skip_install    = False
  ignore_errors   = False
  recreate        = False
  passenv         = {'TOX_REPORTER_TIMESTAMP', 'TOX_WORK_DIR', 'TMPDIR', 'TOX_PARALLEL_ENV', 'LANGUAGE', 'LANG', 'PATH', 'LD_LIBRARY_PATH', 'PIP_INDEX_URL'}
  whitelist_externals = []
  platform        = .*
  sitepackages    = False
  alwayscopy      = False
  pip_pre         = False
  usedevelop      = False
  install_command = ['python', '-m', 'pip', 'install', '{opts}', '{packages}']
  list_dependencies_command = ['python', '-m', 'pip', 'freeze']
  deps            = [pytest, mock]
  commands        = [['/home/ob/do/play/.tox/py37-hello/bin/python', '--version'], ['echo', 'hello']]
  commands_pre    = []
  commands_post   = []
  ignore_outcome  = False
  extras          = []
  depends         = ()
  parallel_show_output = False
[testenv:hello]
  envdir          = /home/ob/do/play/.tox/hello
  setenv          = SetenvDict: {'PYTHONHASHSEED': '1602746531', 'TOX_ENV_NAME': 'hello', 'TOX_ENV_DIR': '/home/ob/do/play/.tox/hello'}
  basepython      = /usr/bin/python
  description     = 
  envtmpdir       = /home/ob/do/play/.tox/hello/tmp
  envlogdir       = /home/ob/do/play/.tox/hello/log
  downloadcache   = None
  changedir       = /home/ob/do/play
  args_are_paths  = True
  skip_install    = False
  ignore_errors   = False
  recreate        = False
  passenv         = {'TOX_REPORTER_TIMESTAMP', 'TOX_WORK_DIR', 'TMPDIR', 'TOX_PARALLEL_ENV', 'LANGUAGE', 'LANG', 'PATH', 'LD_LIBRARY_PATH', 'PIP_INDEX_URL'}
  whitelist_externals = []
  platform        = .*
  sitepackages    = False
  alwayscopy      = False
  pip_pre         = False
  usedevelop      = False
  install_command = ['python', '-m', 'pip', 'install', '{opts}', '{packages}']
  list_dependencies_command = ['python', '-m', 'pip', 'freeze']
  deps            = [pytest, mock]
  commands        = [['/home/ob/do/play/.tox/hello/bin/python', '--version'], ['echo', 'hello']]
  commands_pre    = []
  commands_post   = []
  ignore_outcome  = False
  extras          = []
  depends         = ()
  parallel_show_output = False