1

我已经设置了 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插件加载的错误呢?

4

0 回答 0