web-dev-qa-db-ja.com

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
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テストを実行する前に設定が行われていないようです。

何か案が?

おかげで、ラム

26

メニューに移動file > settings > Django Supportと正しい設定ファイルを選択します。

enter image description here

24
Jan Míšek

私は同じ問題を経験していました。間違ったタイプのテストを実行していることがわかりました。

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内から実行できるようにしました。

0
John Boufford