tox 2.0 から環境変数の分離機能が入った

2015/5/12 にリリースされた tox 2.0.0 から環境変数の分離機能 (environment variable isolation) が搭載されました. これにより tox で実行するテストコード中で環境変数を利用している場合問題が発生することがあります.

Changelog

tox の Changelog には以下のように説明されています.

(new) introduce environment variable isolation: tox now only passes the PATH and PIP_INDEX_URL variable from the tox invocation environment to the test environment and on Windows also SYSTEMROOT, PATHEXT, TEMP and TMP whereas on unix additionally TMPDIR is passed. If you need to pass through further environment variables you can use the new passenv setting, a space-separated list of environment variable names. Each name can make use of fnmatch-style glob patterns. All environment variables which exist in the tox-invocation environment will be copied to the test environment.

これによると tox は PATHPIP_INDEX_URL といった一部の環境変数のみ (実行環境によって異なる) を tox が実行されている環境から, テスト環境に引き継ぎます.

それ以外に追加の環境変数を利用したい場合は passenv という設定に, 利用する環境変数を記述する必要があるとのことです.

設定例

以下のようにして設定します. 環境変数はスペース区切りで列挙し, glob も使えます.

[testenv]
passenv = LANG TRAVIS*

テスト実行時に環境変数にトークン等を設定している場合は, この設定は必須です.

また, Travis CI などの CI サービスを利用している場合, 特定の環境変数を用いて Travis CI で実行されているかを判定するもの (例: coveralls-python) があるので, 必要な環境変数を pass するようにする必要があるので注意が必要です.