pycharm Python单元测试在未配置INSTALLED_APPS设置时出错

gudnpqoy  于 2023-06-06  发布在  PyCharm
关注(0)|答案(2)|浏览(235)

使用PyCharm IDE,我们有一个单元测试,导入Django项目的models模块进行测试。虽然期望顺利通过,但测试总是会遇到错误:
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings. Configure() before accessing settings.
我们对Python单元测试的这一部分是新手,所以我们非常感谢任何提示或建议。

单元测试:

from applicationapp import models
...

class TestElastic(TestCase):
    def test_parse_request(self):
        # expecting happy pass
        # 
        # self. Fail()
        return

完整错误跟踪:

/data/app-py3/venv3.7/bin/python /var/lib/snapd/snap/pycharm-professional/327/plugins/python/helpers/pycharm/_jb_unittest_runner.py --target test_elastic.TestElastic 
Testing started at 2:07 PM ...
Launching unittests with arguments python -m unittest test_elastic.TestElastic in /data/app-py3/APPLICATION/tests

Traceback (most recent call last):
  File "/var/lib/snapd/snap/pycharm-professional/327/plugins/python/helpers/pycharm/_jb_unittest_runner.py", line 35, in <module>
    sys.exit(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 "/data/app-py3/APPLICATION/tests/test_elastic.py", line 6, in <module>
    from applicationapp.soap.elastic import Elastic
  File "/data/app-py3/APPLICATION/applicationapp/soap/elastic.py", line 7, in <module>
    from applicationapp import models
  File "/data/app-py3/APPLICATION/applicationapp/models.py", line 3, in <module>
    from django.contrib.auth.models import User
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    settings.INSTALLED_APPS
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/conf/__init__.py", line 83, in __getattr__
    self._setup(name)
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/conf/__init__.py", line 68, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, 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

Empty suite
tp5buhyn

tp5buhyn1#

CD添加到项目目录中并使用
python manage.py test
请参阅此处的文档:
https://docs.djangoproject.com/en/4.2/topics/testing/overview/#running-tests
或者,您可以将DJANGO_SETTINGS_MODULE设置为环境变量。这一步实际上取决于你所处的环境,但是为了让它工作并让你走上正确的道路,你可能想在终端中做这样的事情:
export DJANGO_SETTINGS_MODULE=dot.path.to.settings.file
请注意,点路径是相对于项目的,所以如果你的主应用程序名为“polls”,那么你的点变量可能是polls.settings
我仔细阅读了你的回溯,我可能是错的,所以请纠正我,但我相信点路径将是applicationapp.settings

uyhoqukh

uyhoqukh2#

根据this post的提示,我们通过在文件开头显式调用django.setup()解决了这个问题。请参阅下面的示例源代码。

from unittest import TestCase

...

import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "applicationapp.settings")
django.setup()
from applicationapp import models

...

class TestElastic(TestCase):
    def test_parse_request(self):
        # expecting happy pass
        # 
        # self. Fail()
        return

相关问题