簡體   English   中英

如何在tox中設置環境變量?

[英]How to set the environment variable in tox?

我有 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

安裝程序.py

$ cat setup.py
from setuptools import setup

setup(name="Tox Testing")

配置文件

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

[testenv]
deps=pytest
commands=py.test

測試環境.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

根據文檔,您可以讓測試環境從[base]繼承值。

[tox]
envlist =
    test1
    test2

[base]
setenv =
    XYZ = 123

[testenv:test1]
deps=pytest
commands=py.test
setenv =
    {[base]setenv}

[testenv:test2]
deps=pytest
commands=py.test
setenv =
    {[base]setenv}

您可以使用以下方法將所有 einvornment 變量傳遞給測試:

[testenv]
passenv = XYZ

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM