Warm tip: This article is reproduced from stackoverflow.com, please click
environment-variables python testing tox

Running tox with different environment variable config

发布于 2020-04-05 23:38:43

I want to run my test with different values of environment variables. I have this tox.ini, which doesn't do what I want:

# tox.ini

[tox]
envlist = py37-{foo,bar}

[testenv]
description = Tests common
setenv =
    MY_VAR=COMMON
commands =
    env

[testenv:foo]
description = Tests foo
setenv =
    MY_VAR=FOO

[testenv:bar]
description = Tests bar
setenv =
    MY_VAR=BAR 

Above ini produced the following output:

$ tox
GLOB sdist-make: 

***

py37-foo run-test: commands[0] | env

***

MY_VAR=COMMON                                 <<<--- MY_VAR=foo is expected

***

py37-bar run-test: commands[0] | env

***

MY_VAR=COMMON                                 <<<--- MY_VAR=bar is expected

Whats wrong?

I use:

  • Win 10.0.18363 Build 18363
  • Python 3.7.4
  • tox: 3.14.0
Questioner
betontalpfa
Viewed
71
betontalpfa 2020-02-01 19:37

The key is the Compressing dependency matrix. This technique results compact, and non-redundant solution:

[tox]
envlist = py37-{foo,bar,baz}

[testenv]
setenv =
    MY_VAR=COMMON
    foo: MY_VAR=FOO
    bar: MY_VAR=BAR
commands =
    env