本文介绍了在PyCharm中运行Django测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在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
测试开始于4:37 PM ...
追溯(最近的最后一次调用):
文件/opt/pycharm-3.0.2/helpers/pycharm/django_test_manage.py ,第18行,在< module>
import django_test_runner
文件/opt/pycharm-3.0.2/helpers/pycharm/django_test_runner.py,第14行在< module>
from django.test.testcases import TestCase
文件/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/test/__init__.py,第5行,< module>
from django.test.client import Client,RequestFactory
文件/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/test/client.py,第11行,< module>
从django.contrib.auth导入验证,登录,注销,get_user_model
文件/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/contrib/auth /__init__.py,第6行,在< module>
from django.middleware.csrf import rotate_token
文件/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/middleware/csrf.py,第14行,< module>
from django.utils.cache import patch_vary_headers
文件/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/utils/cache.py,第26行,< module>
from django.core.cache import get_cache
文件/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/core/cache/__init__.py,第69行在< module>
如果DEFAULT_CACHE_ALIAS不在settings.CACHES中:
文件/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/conf/__init__.py,第54行,__getattr__
self._setup(name)
文件/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/conf/__init__.py,行47,in _setup
%(desc,ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured:请求的设置CACHES,但没有配置设置。在访问设置之前,您必须定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。
处理完成退出代码1
测试在控制台上运行正常 - ./ manage.py run test
在执行测试之前,看起来没有设置什么?
任何想法?
谢谢,
Ram
解决方案
p>转到菜单文件>设置> Django支持
并选择正确的设置文件。
I am trying to run a simple Django test in PyCharm, but its failing with the following stack trace-
/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
The test runs fine on console using- ./manage.py run test
It looks like things are not setup before executing the tests?
Any idea?
Thanks,Ram
解决方案
Go to menu file > settings > Django Support
and select correct settings file.
这篇关于在PyCharm中运行Django测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!