python - 如何在tox中设置环境变量?

标签 python virtualenv tox setenv

我有 tox 2.9.1

$ tox --version
2.9.1 imported from /Library/Python/2.7/site-packages/tox/__init__.pyc
registered plugins:
    tox-pyenv-1.1.0 at /Library/Python/2.7/site-packages/tox_pyenv.pyc

文件结构及内容如下

$ tree .
.
├── setup.py
├── test_env.py
└── tox.ini

0 directories, 3 files

setup.py

$ cat setup.py
from setuptools import setup

setup(name="Tox Testing")

tox.ini

$ cat tox.ini
[tox]
envlist = py35
setenv =
    XYZ = 123

[testenv]
deps=pytest
commands=py.test

test_env.py

$ cat test_env.py
import os

def test_env():
    assert os.getenv('XYZ') == 123

当我运行 tox 命令时,我的测试失败了。

$ tox -v
using tox.ini: /private/tmp/testing/tox.ini
using tox-2.9.1 from /Library/Python/2.7/site-packages/tox/__init__.pyc
GLOB sdist-make: /private/tmp/testing/setup.py
  /private/tmp/testing$ /usr/bin/python /private/tmp/testing/setup.py sdist --formats=zip --dist-dir /private/tmp/testing/.tox/dist >/private/tmp/testing/.tox/log/tox-0.log
py35 reusing: /private/tmp/testing/.tox/py35
py35 inst-nodeps: /private/tmp/testing/.tox/dist/Tox Testing-0.0.0.zip
  /private/tmp/testing$ /private/tmp/testing/.tox/py35/bin/pip install -U --no-deps /private/tmp/testing/.tox/dist/Tox Testing-0.0.0.zip >/private/tmp/testing/.tox/py35/log/py35-16.log
  /private/tmp/testing$ /private/tmp/testing/.tox/py35/bin/pip freeze >/private/tmp/testing/.tox/py35/log/py35-17.log
py35 installed: py==1.4.34,pytest==3.2.3,Tox-Testing==0.0.0
py35 runtests: PYTHONHASHSEED='3929104870'
py35 runtests: commands[0] | py.test
  /private/tmp/testing$ /private/tmp/testing/.tox/py35/bin/py.test
================================================================================ test session starts ================================================================================
platform darwin -- Python 3.5.2, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /private/tmp/testing, inifile:
collected 1 item

test_env.py F

===================================================================================== FAILURES ======================================================================================
_____________________________________________________________________________________ test_env ______________________________________________________________________________________

    def test_env():
>       assert os.getenv('XYZ') == 123
E       AssertionError: assert None == 123
E        +  where None = <function getenv at 0x1053daae8>('XYZ')
E        +    where <function getenv at 0x1053daae8> = os.getenv

test_env.py:4: AssertionError
============================================================================= 1 failed in 0.11 seconds ==============================================================================
ERROR: InvocationError: '/private/tmp/testing/.tox/py35/bin/py.test'
______________________________________________________________________________________ summary ______________________________________________________________________________________
ERROR:   py35: commands failed

来自 setenv 的文档看来应该设置环境变量。

最佳答案

setenv 必须在[testenv]:

[tox]
envlist = py35

[testenv]
deps=pytest
commands=py.test
setenv =
    XYZ = 123

关于python - 如何在tox中设置环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47332874/

相关文章:

python - 使用 pandas 高效读取大型 CSV 文件而不会崩溃

python - "python"virtualenv activate后仍然运行系统版本

python - ModuleNotFoundError : No module named <modulename> after pip install

python-3.x - 如何为python模块为 'tox'运行 'py.test'命令?

python - tox 下的测试不一定使用已安装的代码

javascript - 管理对象实例

Python模拟对象实例化

python - 共享 Python virtualenv 环境

python - 测试pytest插件时如何获得覆盖率报告?

python - pandas (sub)Dataframe 中的最大值和最小值