Specify Python version-specific testenv settings, when platform is used

Viewed 146

How can I set a testenv that specifies settings for a specific Python version? As a minimal example,

[tox]
envlist = py{36,37,38,39}-{mylinux,macos,mywindows}
skipsdist = True

[testenv]
platform = mylinux: linux
           mymacos: darwin
           mywindows: win32
setenv =
    mylinux: PLATFORM = linux
    mymacos: PLATFORM = macos
    mywindows: PLATFORM = windows

commands =
    {envpython} -c "print('{env:PLATFORM}')"  # This env is used in setup.py
    # other commands

[testenv:py36]
deps =
    pydantic  # No dataclasses until py37

If I run tox -e py36-mylinux, pydantic won't be installed. If I run tox -e py36, it would say

ERROR:   py36: unresolvable substitution(s):
    commands: 'PLATFORM'
Environment variables are missing or defined recursively.

How can I specify testenv settings for a particular Python version, without having to specify all other info in environment names (especially when the envlist gets long)?

1 Answers

The same way; put this in [testenv] (and remove [testenv:py36]):

deps =
    py36: pydantic  # No dataclasses until py37

Verify config with tox --showconfig

PS. tox doesn't combine environments; once you have a separate [testenv:py36] it will not be combined with mylinux.

Related