在PyCharm中运行Django测试

Ram*_*wal 25 python testing django pycharm

我试图在PyCharm中运行一个简单的Django测试,但它失败了以下堆栈跟踪 -

/home/ramashishb/local/pyenv/testenv/bin/python /opt/pycharm-3.0.2/helpers/pycharm/django_test_manage.py test snippets.SimpleTest.test_simple /home/ramashishb/mine/learn/django-rest/django-rest-tutorial
Testing started at 4:37 PM ...
Traceback (most recent call last):
  File "/opt/pycharm-3.0.2/helpers/pycharm/django_test_manage.py", line 18, in <module>
    import django_test_runner
  File "/opt/pycharm-3.0.2/helpers/pycharm/django_test_runner.py", line 14, in <module>
    from django.test.testcases import TestCase
  File "/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/test/__init__.py", line 5, in <module>
    from django.test.client import Client, RequestFactory
  File "/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/test/client.py", line 11, in <module>
    from django.contrib.auth import authenticate, login, logout, get_user_model
  File "/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 6, in <module>
    from django.middleware.csrf import rotate_token
  File "/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/middleware/csrf.py", line 14, in <module>
    from django.utils.cache import patch_vary_headers
  File "/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/utils/cache.py", line 26, in <module>
    from django.core.cache import get_cache
  File "/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/core/cache/__init__.py", line 69, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/conf/__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.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.

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

测试在控制台上运行良好 - ./manage.py run test 看起来在执行测试之前没有设置好的东西?

任何的想法?

谢谢,拉姆

Jan*_*šek 26

转到菜单file > settings > Django Support并选择正确的设置文件.

在此输入图像描述

  • 谢谢Jan.我发现我必须在运行/调试配置中明确设置DJANGO_SETTINGS_MODULE才能运行测试. (4认同)
  • 我必须删除要尝试运行的测试的现有Python unittest运行配置,然后才能将其作为Django测试运行。我不需要设置任何环境变量。 (2认同)
  • @kolen如果您仍然感兴趣(超过2年:d),我只是找到了一种方法(解决方法),可以通过右键单击来运行它们:http://stackoverflow.com/questions/42989471/run-debug-a django-applications-units从鼠标右键单击上下文我 (2认同)