5

Having troubles running tests in PyCharm.

manage.py test works fine.

But if I run test in PyCharm Django Test getting following error:

AttributeError: 'module' object has no attribute 'ROOT_URLCONF'

Django tests Run\Debug configuration:

DJANGO_SETTINGS_MODULE=project.settings.test

Django Preferences

Settings: project/settings/test.py

Manage Script: project/manage.py

Test

from django.test import SimpleTestCase
from rest_framework import status


class AccountTestCase(SimpleTestCase):

    def test_current_account(self):
        url = '/api/accounts/current/'
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_301_MOVED_PERMANENTLY)

StackTrace

Error
    packages/django/core/handlers/base.py", line 113, in get_response
urlconf = settings.ROOT_URLCONF
  File "/lib/python2.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
return getattr(self._wrapped, name)
  File "/lib/python2.7/site-packages/django/conf/__init__.py", line 173, in __getattr__
return getattr(self.default_settings, name)
AttributeError: 'module' object has no attribute 'ROOT_URLCONF'

Would appreciate any help.

Vladimir Nani
  • 2,774
  • 6
  • 31
  • 52
  • Have you seen [this question/answer](http://stackoverflow.com/questions/12839213/attributeerror-settings-object-has-no-attribute-root-urlconf)? – Jens Astrup Nov 23 '16 at 02:09
  • yes, and if i run test in terminal it works, it has something to do with the way i should configure PyCharm. – Vladimir Nani Nov 23 '16 at 07:50

2 Answers2

6

Ok got it.

You have to mark root folder in PyCharm as "sources root"

enter image description here

enter image description here

Then I guess it adds it to PYTHONPATH

Vladimir Nani
  • 2,774
  • 6
  • 31
  • 52
2

Verify the Pycharm Tools menu has an item "Run manage.py Task...". If not, configuration changes are needed in File > Settings > Languages & Frameworks > Django. Additionally, if using a remote database, permission to create databases is required.

PyCharm Django projects require some setup to fully use all the IDE features. Many actions run fine, but Django testing does not. When running test.py, the following console messages indicate additional project setup is needed:

  • There is no such settings file settings
  • AttributeError: module 'django.conf.global_settings' has no attribute 'ROOT_URLCONF'
Nomen Nescio
  • 347
  • 1
  • 7
  • This one did it for me. I had accidentally renamed my `settings.py` file which apparently triggered PyCharm to lose track of it. Re-adding a reference solved the issue. – alphazwest Jul 16 '23 at 12:48