能否为同一配置绑定多个testenv标签以复用tox脚本?
Yes, absolutely! Tox fully supports grouping multiple test environment names in a single [testenv:<names>] section to eliminate repetitive configuration code—this is exactly the right approach to clean up your current setup.
How to implement your proposed approach
Your example syntax is valid. You can group environments that share identical settings (like your Python 3.x Jenkins envs) under one section. For your specific case, split the Python 2 and Python 3 environments into two grouped sections since they only differ in the basepython value:
# Group Python 3.x Jenkins environments [testenv:py3.6.4-jenkins,py3.5.4-jenkins,py3-jenkins] basepython = python3 commands = {toxinidir}/jenkins.sh setenv = STANFORD_MODELS = {homedir}/third/stanford-parser/ STANFORD_PARSER = {homedir}/third/stanford-parser/ STANFORD_POSTAGGER = {homedir}/third/stanford-postagger/ # Separate Python 2.7 Jenkins environment (uses different basepython) [testenv:py2.7.14-jenkins] basepython = python commands = {toxinidir}/jenkins.sh setenv = STANFORD_MODELS = {homedir}/third/stanford-parser/ STANFORD_PARSER = {homedir}/third/stanford-parser/ STANFORD_POSTAGGER = {homedir}/third/stanford-postagger/
Even better: Use Tox's default testenv inheritance
To cut down on duplication even more, define shared settings in a top-level [testenv] section, then override only the differing values in specific environment groups. This way you won’t have to repeat commands and setenv across sections:
# Default shared configuration for all Jenkins envs [testenv] commands = {toxinidir}/jenkins.sh setenv = STANFORD_MODELS = {homedir}/third/stanford-parser/ STANFORD_PARSER = {homedir}/third/stanford-parser/ STANFORD_POSTAGGER = {homedir}/third/stanford-postagger/ # Override basepython for Python 3.x envs [testenv:py3.6.4-jenkins,py3.5.4-jenkins,py3-jenkins] basepython = python3 # Override basepython for Python 2.7 env [testenv:py2.7.14-jenkins] basepython = python
This inheritance model keeps your tox.ini clean and maintainable—any changes to shared commands or environment variables only need to be made once in the [testenv] section.
内容的提问来源于stack exchange,提问作者alvas

