32

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

Ramashish Baranwal
  • 6,734
  • 2
  • 16
  • 14

2 Answers2

32

Go to menu file > settings > Django Support and select correct settings file.

enter image description here

Jan Míšek
  • 1,595
  • 1
  • 14
  • 22
  • 4
    Thanks Jan. I found I had to explicitly set DJANGO_SETTINGS_MODULE in Run/Debug configurations for the tests to run. – Ramashish Baranwal Dec 30 '13 at 15:31
  • 2
    I had to delete existing Python unittest run configurations for the tests I was trying to run before I could run them as Django tests. I did not need to set any environment variables. – Nathan Jan 06 '14 at 22:11
  • 1
    Is it possible to run Django tests in Pycharm community edition (where there's no Django plugin)? (With more integrated way than just running `manage.py test` as Run configuration) – kolen Dec 08 '14 at 11:24
  • In Intellij IDEA, this is in the project settings, but yes, this is the solution! – mlissner Mar 31 '16 at 19:02
  • Just in case this helps someone else: When setting the DJANGO_SETTINGS_MODULE in Run/Debug configuration in IntelliJ IDEA, you shouldn't specify settings.py but instead just settings because importing by file path is not supported. – Henok Getachew Apr 14 '16 at 07:02
  • 2
    @kolen If you're still interested (after 2+ years :d ), I just found a way (workaround) to run them via right-click: http://stackoverflow.com/questions/42989471/run-debug-a-django-applications-unittests-from-the-mouse-right-click-context-me – CristiFati Mar 24 '17 at 20:22
  • 7 years later this post just concluded hours of search...I was getting a strange ContentTypes model not in INSTALLED_APPS error when trying to run my test with pycharm. – Karnalta Feb 14 '20 at 19:42
0

I was experiencing the same problem. I found that I was running the wrong type of test.

import unittest
class MySampleTest(unittest.TestCase):

Cause the error

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.

Changing to the import to

from django.test import SimpleTestCase

Class MySampleTest(SimpleTestCase):

allowed my test to run from within pycharm.

Laura
  • 5,088
  • 3
  • 26
  • 37
  • This does not answer the question, SimpleTestCase is used when you want to disable database queries by default and run non-db tests. Ref: https://docs.djangoproject.com/en/2.2/topics/testing/tools/#simpletestcase - PyCharm triggers these tests by default as python tests and not django test. – Deep Jun 23 '19 at 07:22