首页 > 解决方案 > 如何使用本地设置在调试模式下使用 PyCharm 在 Django 中运行 python 单元测试?

问题描述

我的Django项目有本地设置。./manage.py runserver --settings=settings.local我用命令运行应用程序。

我有以下设置Unittests 在此处输入图像描述

但是当我按下Debug Unittests for test.t...我得到错误

/home/m0nte-cr1st0/.virtualenvs/onedeeds/bin/python /home/m0nte-cr1st0/.local/share/umake/ide/pycharm/helpers/pycharm/_jb_unittest_runner.py --target core.test.test_views.ReactivateAccountViewTestCase
Launching unittests with arguments python -m unittest core.test.test_views.ReactivateAccountViewTestCase in /home/m0nte-cr1st0/pet_projects/onedeeds/onedeeds/core

Traceback (most recent call last):
  File "/home/m0nte-cr1st0/.local/share/umake/ide/pycharm/helpers/pycharm/_jb_unittest_runner.py", line 35, in <module>
    main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
  File "/usr/local/lib/python3.7/unittest/main.py", line 100, in __init__
    self.parseArgs(argv)
  File "/usr/local/lib/python3.7/unittest/main.py", line 147, in parseArgs
    self.createTests()
  File "/usr/local/lib/python3.7/unittest/main.py", line 159, in createTests
    self.module)
  File "/usr/local/lib/python3.7/unittest/loader.py", line 220, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/local/lib/python3.7/unittest/loader.py", line 220, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/local/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/home/m0nte-cr1st0/pet_projects/onedeeds/onedeeds/core/test/test_views.py", line 6, in <module>
    from rest_framework.authtoken.models import Token
  File "/home/m0nte-cr1st0/.virtualenvs/onedeeds/lib/python3.7/site-packages/rest_framework/authtoken/models.py", line 9, in <module>
    class Token(models.Model):
  File "/home/m0nte-cr1st0/.virtualenvs/onedeeds/lib/python3.7/site-packages/rest_framework/authtoken/models.py", line 15, in Token
    settings.AUTH_USER_MODEL, related_name='auth_token',
  File "/home/m0nte-cr1st0/.virtualenvs/onedeeds/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/home/m0nte-cr1st0/.virtualenvs/onedeeds/lib/python3.7/site-packages/django/conf/__init__.py", line 64, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting AUTH_USER_MODEL, 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

设置/base.py

AUTH_USER_MODEL = "core.User"

设置/local.py

from .base import *

项目结构

onedeeds/
  onedeeds/
    app1/
      ...
      test/
    app2/
      ...
      test/
    settings/
      base.py
      local.py
    manage.py

标签: pythondjangounit-testingintellij-ideapycharm

解决方案


推荐阅读