在tox中是否可能使tox -e hello触发hello环境并在全局envlist上运行?对py2和py3运行tox可以正确运行。运行tox -e hello仅对py3运行。[tox]skipsdist = Trueenvlist = py{27,37}[testenv]deps = pytest !py27: mockcommands = {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吗?考虑到其他所有关于毒药的灵活性和可重用性,这似乎很笨拙。我觉得我想念什么。 最佳答案 这里有两点要理解:envlist是[tox]部分中的global setting。在[testenv:hello]中定义它无效envlist定义或生成env名称的列表。可以通过提供一个简单的逗号分隔列表来静态完成此操作,也可以像您一样,通过特殊语法{...}根据提供的因子组合生成列表。两种情况下的结果都是一个环境名称列表,然后将在单独的virtualenvs / venvs中执行这些环境名称。因此,要明确地说:envlist是env名称的简单列表-别无其他。在envs中执行这些名称(或其部分(也称为因子))时正在执行的操作。如果在不使用-e的情况下调用tox,则将执行所有这些环境。在您的示例中,您使用{<factorX>,<factorY>}语法generating an envlist,该语法基于命令行外壳程序所提供的相同原理。如果您在Tox配置中要求生成的环境,结果将是:$ tox -apy27-hellopy37-hellohello这可能已经表明您的配置有些问题,因为我认为您不希望使用不合格的hello env。因此,通过生成创建的envlist并使用hello语法静态提供[testenv:hello] testenv来进行的工作是将它们混合在一起,而不会产生想要的结果。因此,让我们调整您的tox.ini来摆脱不必要的静态定义的testenv:hello。我们也摆脱了本地envlist键,因为它始终无效。它没有引发错误,因为显式允许使用envs中的任意命名的键,有时这些键很有用。[tox]skipsdist = Trueenvlist = py{27,37}-hello[testenv]deps = pytest !py27: mock# do not warn that echo is a command not in the venvwhitelist_externals = echocommands = {envpython} --version hello: echo hello为确保只运行hello命令,当env具有hello因子时,可以使用<factor>[,<factor>]: <some command>指定在env名称包含特定factor时应运行的内容(名称部分之间用)正在生成环境名称列表,这些列表随后将用作正在执行的具体环境的信息。如果我再次在更改的ini上调用-,则会得到:py27-hellopy37-hello现在,我可以致电(例如) tox -a甚至tox -e py27-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关于python - 如何在其他环境下运行tox -e myenv?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56195146/ 10-14 16:00