Pycharm django 测试设置

Vla*_*ani 5 testing django pycharm

在 PyCharm 中运行测试时遇到问题。

manage.py test 工作正常。

但是,如果我在 PyCharm Django 测试中运行测试,则会出现以下错误:

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

Django 测试 Run\Debug 配置:

DJANGO_SETTINGS_MODULE=project.settings.test

Django 偏好设置

设置: project/settings/test.py

管理脚本: project/manage.py

测试

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)
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪

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'
Run Code Online (Sandbox Code Playgroud)

将不胜感激任何帮助。

Vla*_*ani 5

好的,我知道了。

您必须将 PyCharm 中的根文件夹标记为“源根”

在此处输入图片说明

在此处输入图片说明

然后我想它会将它添加到 PYTHONPATH