我正在为我的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'
更新了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
或py.test
并且 ini文件为空时,未检测到 pytest.ini 文件:
编辑3:什么对我有用
更新了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
答案 0 :(得分:0)
我不确定,但是您的测试似乎无法正确导入django应用程序及其设置。 最好的模拟方法是在脚本之前包含(导入)manage.py中存在的任何内容(是否需要获取wsgi应用程序是另一回事)。我没有使用混音器的经验,但是错误似乎未按正确的顺序加载django应用程序。
有关语法,请参考this。 重要的是在这里给出正确的路径
os.environ.setdefault("DJANGO_SETTINGS_MODULE", correctpath)
还提供正确的设置路径,即确保设置具有所有必需的东西(例如已安装的应用程序)和所有其他东西(例如db conf),以使其正常工作,即使没有,您也可以从工作设置文件中导入丢失的东西像您本地的或生产的。
答案 1 :(得分:0)
此属性的 INSTALLED_APPS 问题配置在哪里,或者您不在应用程序标签中的app.py中。
答案 2 :(得分:0)
就我而言,升级到Django 1.11后,这开始神秘地发生。解决方案是使用pytest的--ds
参数告诉pytest在哪里可以找到我们的django设置。因此我们的命令来自:
pytest path/to/some/test.py
收件人:
pytest --ds proj.settings path/to/some/test.py
很奇怪,但是现在可以了。