我有毒
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")
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
根据
docs,您可以使测试环境继承
[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
我在使用
tox
和 pytest
的组合来测试一些导入 pyautogui
的代码时遇到了这个问题。即使我使用setenv
来设置$DISPLAY
,我发现我也需要设置$HOME
。我设置了$HOME
之后,它还是坏了,因为我没有$HOME/.Xauthority
文件。
对我来说更好的解决方案是模拟
pyautogui
,因为无论如何我都不希望鼠标在单元测试期间移动。
为此,我在
tests
目录 (tests/conftest.py
) 的根目录下创建了一个固定文件,其中包含以下内容:
import sys
from unittest import mock
# Mock pyautogui to prevent it breaking on import
sys.modules["pyautogui"] = mock.MagicMock()
只要您记住
pyautogui
及其所有函数/类都将在您的所有测试中进行模拟,就可以很好地工作。对于我(以及我认为的大多数人)来说,这是一个功能,而不是一个错误,因为这意味着您不必在单独的测试中不断模拟这些功能。