问题
I can see in my travis build log that env variables are exported correctly :
Setting environment variables from .travis.yml
$ export K_API_KEY=[secure]
$ export K_PRIVATE_KEY=[secure]
$ export TOXENV=py27
However they aren't picked in my tests which use a basic config.py file that just should get the env variables this way (API_KEY = os.environ['K_API_KEY']
), see relevant travis log:
$ source ~/virtualenv/python2.7/bin/activate
$ python --version
Python 2.7.9
$ pip --version
pip 6.0.7 from /home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages (python 2.7)
install
1.34s$ pip install -U tox
Collecting tox
Downloading tox-2.3.1-py2.py3-none-any.whl (40kB)
100% |################################| 40kB 1.2MB/s
Collecting virtualenv>=1.11.2 (from tox)
Downloading virtualenv-15.0.2-py2.py3-none-any.whl (1.8MB)
100% |################################| 1.8MB 262kB/s
Collecting py>=1.4.17 from https://pypi.python.org/packages/19/f2/4b71181a49a4673a12c8f5075b8744c5feb0ed9eba352dd22512d2c04d47/py-1.4.31-py2.py3-none-any.whl#md5=aa18874c9b4d1e5ab53e025008e43387 (from tox)
Downloading py-1.4.31-py2.py3-none-any.whl (81kB)
100% |################################| 86kB 3.5MB/s
Collecting pluggy<0.4.0,>=0.3.0 (from tox)
Downloading pluggy-0.3.1-py2.py3-none-any.whl
Installing collected packages: pluggy, py, virtualenv, tox
Found existing installation: py 1.4.26
Uninstalling py-1.4.26:
Successfully uninstalled py-1.4.26
Successfully installed pluggy-0.3.1 py-1.4.31 tox-2.3.1 virtualenv-15.0.2
$ tox
GLOB sdist-make: /home/travis/build/euri10/pykraken/setup.py
py27 create: /home/travis/build/euri10/pykraken/.tox/py27
py27 installdeps: -r/home/travis/build/euri10/pykraken/requirements_dev.txt
py27 inst: /home/travis/build/euri10/pykraken/.tox/dist/pykraken-0.1.0.zip
py27 installed: alabaster==0.7.8,argh==0.26.2,Babel==2.3.4,bumpversion==0.5.3,cffi==1.6.0,coverage==4.0,cryptography==1.3.2,docutils==0.12,enum34==1.1.6,flake8==2.4.1,idna==2.1,ipaddress==1.0.16,Jinja2==2.8,MarkupSafe==0.23,mccabe==0.3.1,pathtools==0.1.2,pep8==1.7.0,pluggy==0.3.1,py==1.4.31,pyasn1==0.1.9,pycparser==2.14,pyflakes==0.8.1,Pygments==2.1.3,pykraken==0.1.0,pytest==2.8.3,pytz==2016.4,PyYAML==3.11,requests==2.10.0,six==1.10.0,snowballstemmer==1.2.1,Sphinx==1.3.1,sphinx-rtd-theme==0.1.9,tox==2.1.1,virtualenv==15.0.2,watchdog==0.8.3
py27 runtests: PYTHONHASHSEED='2032885705'
py27 runtests: commands[0] | py.test --basetemp=/home/travis/build/euri10/pykraken/.tox/py27/tmp
============================= test session starts ==============================
platform linux2 -- Python 2.7.9, pytest-2.8.3, py-1.4.31, pluggy-0.3.1
rootdir: /home/travis/build/euri10/pykraken, inifile:
collected 0 items / 2 errors
==================================== ERRORS ====================================
____________________ ERROR collecting tests/test_private.py ____________________
tests/test_private.py:5: in <module>
from pykraken.config import PROXY, API_KEY, PRIVATE_KEY
pykraken/config.py:4: in <module>
API_KEY = os.environ['K_API_KEY']
.tox/py27/lib/python2.7/UserDict.py:23: in __getitem__
raise KeyError(key)
E KeyError: 'K_API_KEY'
I suspect it's my tox.ini (see below) that doesn't pick those variables, but not sure, any idea ?
[tox]
envlist = py27, flake8
; envlist = py26, py27, py33, py34, py35
[flake8]
max-line-length= 100
exclude= tests/*
[testenv]
setenv =
PYTHONPATH = {toxinidir}:{toxinidir}/pykraken
deps =
-r{toxinidir}/requirements_dev.txt
commands =
py.test --basetemp={envtmpdir}
回答1:
As righlty pointed by @jonrsharpe the solution is to use the passenv
option in tox.ini as described in the documentation
[testenv]
setenv =
PYTHONPATH = {toxinidir}:{toxinidir}/pykraken
passenv =
K_API_KEY
K_PRIVATE_KEY
来源:https://stackoverflow.com/questions/37521881/travis-secure-env-variables-not-used-in-tox