我已经设置了pytest(3.1.0)和pytest-django(3.1.2)来运行一些测试。这在Ubuntu 17.04和OSX 10.12.2上很有用,但在我们的测试环境(Ubuntu 12.04.5)上却没有。
在查看pytest --version
时,会在一切按预期工作的环境中检测到插件,但在测试服务器上未列出插件。这会导致工作环境的ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
输出错误:
$ venv/bin/pytest --version
This is pytest version 3.1.0, imported from /home/me/code/myproject/myproject_device/venv/local/lib/python2.7/site-packages/pytest.pyc
setuptools registered plugins:
pytest-django-3.1.2 at /home/me/code/myproject/myproject_device/venv/local/lib/python2.7/site-packages/pytest_django/plugin.py
在搜索详细信息时,我发现了一些关于pytest插件的导入错误抑制的pytest问题(fi https://github.com/pytest-dev/pytest/issues/1307),但这些问题涉及pytest 3的行为更改,其中错误或警告会对于这些问题是可见的。但是除了django本身的ImproperlyConfigured异常之外,我没有看到任何错误。完整输出:
$ ./vagrant_venv/bin/pytest --version
This is pytest version 3.1.0, imported from /vagrant/vagrant_venv/local/lib/python2.7/site-packages/pytest.pyc
$ ./vagrant_venv/bin/pytest -rw -s src
============================= test session starts ==============================
platform linux2 -- Python 2.7.12, pytest-3.1.0, py-1.4.33, pluggy-0.4.0
rootdir: /vagrant, inifile: pytest.ini
collected 36 items / 1 errors
==================================== ERRORS ====================================
_________ ERROR collecting src/myproject/myproject/tests/test_views.py _________
src/myproject/myproject/tests/test_views.py:6: in <module>
from ui.views import controlview
src/myproject/ui/views/__init__.py:2: in <module>
from controlviewdata import ControlViewData
src/myproject/ui/views/controlviewdata.py:5: in <module>
from django.views.decorators.csrf import csrf_exempt
vagrant_venv/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:1: in <module>
from django.middleware.csrf import CsrfViewMiddleware, get_token
vagrant_venv/local/lib/python2.7/site-packages/django/middleware/csrf.py:14: in <module>
from django.utils.cache import patch_vary_headers
vagrant_venv/local/lib/python2.7/site-packages/django/utils/cache.py:26: in <module>
from django.core.cache import caches
vagrant_venv/local/lib/python2.7/site-packages/django/core/cache/__init__.py:34: in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
vagrant_venv/local/lib/python2.7/site-packages/django/conf/__init__.py:46: in __getattr__
self._setup(name)
vagrant_venv/local/lib/python2.7/site-packages/django/conf/__init__.py:40: in _setup
% (desc, ENVIRONMENT_VARIABLE))
E ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 7.25 seconds ============================
我可以在ubuntu/precise64
的流浪盒中重现这一点而不会出现问题。我尝试将python 2.7升级到更新的版本,但也没有任何区别。
那么如何追踪阻止pytest-django
插件加载的错误?