首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在PyCharm中运行Django测试

在PyCharm中运行Django测试
EN

Stack Overflow用户
提问于 2013-12-05 19:40:40
回答 2查看 18.2K关注 0票数 33

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

代码语言:javascript
复制
/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

测试在控制台上使用- ./manage.py run test运行得很好,看起来在执行测试之前没有进行设置?

有什么想法吗?

谢谢,公羊

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-30 17:25:54

进入menu file > settings > Django Support并选择正确的设置文件。

票数 32
EN

Stack Overflow用户

发布于 2019-04-04 02:50:17

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

代码语言:javascript
复制
import unittest
class MySampleTest(unittest.TestCase):

导致错误

代码语言:javascript
复制
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.

更改为导入到

代码语言:javascript
复制
from django.test import SimpleTestCase

Class MySampleTest(SimpleTestCase):

允许我的测试从pycharm内部运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20399046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档