Python 在PyCharm中运行Django测试

Python 在PyCharm中运行Django测试,python,django,testing,pycharm,Python,Django,Testing,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 Testi

我试图在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
/home/ramashishb/local/pyenv/testenv/bin/python/opt/pycharm-3.0.2/helpers/pycharm/django_test\u manage.py test snippets.SimpleTest.test\u simple/home/ramashishb/mine/learn/django rest/django rest教程
测试在下午4:37开始。。。
回溯(最近一次呼叫最后一次):
文件“/opt/pycharm-3.0.2/helpers/pycharm/django_test_manage.py”,第18行,在
导入django_测试_runner
文件“/opt/pycharm-3.0.2/helpers/pycharm/django_test_runner.py”,第14行,在
从django.test.testcases导入TestCase
文件“/home/ramashishb/local/pyenv/testenv/lib/python2.7/site packages/django/test/_init__.py”,第5行,在
从django.test.client导入客户端,RequestFactory
文件“/home/ramashishb/local/pyenv/testenv/lib/python2.7/site packages/django/test/client.py”,第11行,在
从django.contrib.auth导入身份验证、登录、注销、获取用户模型
文件“/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/contrib/auth/_-init__;.py”,第6行
从django.middleware.csrf导入rotate_令牌
文件“/home/ramashishb/local/pyenv/testenv/lib/python2.7/site packages/django/middleware/csrf.py”,第14行,在
从django.utils.cache导入补丁\u vary\u头
文件“/home/ramashishb/local/pyenv/testenv/lib/python2.7/site packages/django/utils/cache.py”,第26行,在
从django.core.cache导入获取缓存
文件“/home/ramashishb/local/pyenv/testenv/lib/python2.7/site packages/django/core/cache/_init__.py”,第69行,in
如果默认\u缓存\u别名不在settings.CACHES中:
文件“/home/ramashishb/local/pyenv/testenv/lib/python2.7/site-packages/django/conf/_-init___;.py”,第54行,在__
自我设置(名称)
文件“/home/ramashishb/local/pyenv/testenv/lib/python2.7/site packages/django/conf/_init__.py”,第47行,在安装程序中
%(描述,环境变量)
django.core.exceptions.ImpropertlyConfigured:请求设置缓存,但未配置设置。在访问设置之前,必须定义环境变量DJANGO_SETTINGS_MODULE或调用SETTINGS.configure()。
进程已完成,退出代码为1
使用-
/manage.py run test
似乎在执行测试之前没有设置好东西

有什么想法吗

谢谢,
Ram

进入菜单
文件>设置>Django支持
并选择正确的设置文件


我也遇到了同样的问题。我发现我运行了错误的测试类型

import unittest
class MySampleTest(unittest.TestCase):
造成错误

django.core.exceptions.ImproperlyConfigured: Requested setting API_BASE_URL, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
更改为导入到

from django.test import SimpleTestCase

Class MySampleTest(SimpleTestCase):

允许我的测试从
pycharm
中运行

您是否在pycharm项目设置中启用了Django支持,并告诉它要使用哪个设置文件?是的。Django支持已启用。我发现我必须显式设置DJANGO_SETTINGS_MODULE才能运行测试。感谢Jan。我发现我必须在运行/调试配置中显式设置DJANGO_SETTINGS_MODULE才能运行测试。我必须删除我尝试运行的测试的现有Python unittest运行配置,然后才能将它们作为DJANGO测试运行。我不需要设置任何环境变量。可以在Pycharm社区版(没有Django插件的地方)中运行Django测试吗?(比只运行
manage.py test
as Run configuration更具集成性)在Intellij IDEA中,这是在项目设置中,但是的,这是解决方案@kolen如果您仍然感兴趣(2年多之后:d),我刚刚找到了一种通过右键单击运行它们的方法(解决方法):这并不能回答问题,当您希望在默认情况下禁用数据库查询并运行非db测试时,将使用SimpleTestCase。Ref:-PyCharm默认情况下作为python测试而不是django测试触发这些测试。