gpt4 book ai didi

django.core.exceptions.AppRegistryNotReady : Apps aren't loaded yet with Django pytest

转载 作者:行者123 更新时间:2023-12-04 16:46:22 27 4
gpt4 key购买 nike

我正在为我的 Django 2.0 写测试应用。

我正在使用 mixer目录结构就像

project
|- src
|- contacts
|- migrations
|- tests
|- __init__.py
|- test_models.py
|- __init__.py
|- models.py
|- apps.py
|- koober <---- (main app)
|- settings
|- __init__.py
|- local.py
|- production.py
|- __init__.py
|- test_settings.py
|- urls.py
|- wsgi.py
|- .coveragerc
|- manage.py
|- pytest.ini
|- other_files
|- not_related_to_project
|- Pipfile
|- Pipfile.lock
|- Procfile

当我从 project 运行时目录
project$ pipenv run py.test

它给出了错误
============================================================================= test session starts ==============================================================================
platform linux -- Python 3.6.5, pytest-3.6.1, py-1.5.3, pluggy-0.6.0
rootdir: /home/anuj/code/python/koober, inifile:
plugins: django-3.3.0, cov-2.5.1
collected 0 items / 1 errors

==================================================================================== ERRORS ====================================================================================
______________________________________________________________ ERROR collecting src/contacts/tests/test_models.py ______________________________________________________________
src/contacts/tests/test_models.py:2: in <module>
from mixer.backend.django import mixer
../../../.local/share/virtualenvs/koober-MOd9u5HA/lib/python3.6/site-packages/mixer/backend/django.py:11: in <module>
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation # noqa
../../../.local/share/virtualenvs/koober-MOd9u5HA/lib/python3.6/site-packages/django/contrib/contenttypes/fields.py:3: in <module>
from django.contrib.contenttypes.models import ContentType
../../../.local/share/virtualenvs/koober-MOd9u5HA/lib/python3.6/site-packages/django/contrib/contenttypes/models.py:134: in <module>
class ContentType(models.Model):
../../../.local/share/virtualenvs/koober-MOd9u5HA/lib/python3.6/site-packages/django/db/models/base.py:100: in __new__
app_config = apps.get_containing_app_config(module)
../../../.local/share/virtualenvs/koober-MOd9u5HA/lib/python3.6/site-packages/django/apps/registry.py:244: in get_containing_app_config
self.check_apps_ready()
../../../.local/share/virtualenvs/koober-MOd9u5HA/lib/python3.6/site-packages/django/apps/registry.py:127: in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
E django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================== 1 error in 0.31 seconds ============================================================================

当我从 project/src 运行相同的命令时文件,它给出了错误
ImportError: No module named 'koober.test_settings.py'; 'koober.test_settings' is not a package

我检查了任何错误
project$ pipenv run python src/manage.py check

它给
System check identified no issues (0 silenced).

pipenv run python src/manage.py runserver工作正常。

的内容test_settings.py
from koober.settings import *

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
}

EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'

Updated test_settings.py


import os

import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "koober.settings")
django.setup()

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
}

EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'

pytest.ini 的内容
[pytest]

DJANGO_SETTINGS_MODULE = koober.test_settings.py
addopts = --nomigrations --cov=. --cov-report=html

src/contacts/tests/test_models.py 的内容
import pytest
from mixer.backend.django import mixer

from django.contrib.auth.models import User

pytestmark = pytest.mark.django_db


class TestContact:
def test_model(self):
user = mixer.blend(User)

另外,可以在 pytest 日志中看到, pytest.ini 运行时未检测到文件 pytestpy.test inifile: 是空的

Edit 3: What works for me



更新了 test_settings.py
from .settings import *

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
}

EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'

/src 运行的命令目录
pipenv run pytest --ds=koober.test_settings

最佳答案

我不确定,但您的测试似乎没有正确导入 django 应用程序及其设置。
模拟它的最佳方法是在脚本之前包含(导入)manage.py 中的任何内容(是否需要包含 get wsgi 应用程序是另一回事)。我没有使用混合器的经验,但错误看起来像是 django 应用程序没有按正确的顺序加载。

引用 this对于语法。
重要的是在这里给出正确的路径

os.environ.setdefault("DJANGO_SETTINGS_MODULE", correctpath)

还要提供正确的设置路径,即确保设置具有所有必需的东西,例如已安装的应用程序和所有其他东西,例如 db conf 以使其工作,即使它没有,您也可以从工作设置文件中导入缺少的文件,例如本地或生产的。

关于django.core.exceptions.AppRegistryNotReady : Apps aren't loaded yet with Django pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50946363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com