python-使用pytest测试django应用程序时出错,没有安装带有此标签的应用程序
发布时间:2022-04-23 01:37:23 274
相关标签: # node.js
我正在用pytest为django应用程序实现测试。我添加了decorator@pytest。做记号django_db访问我的test_函数,这样我就可以访问我的数据库(sqlite3)。当我运行一个简单的断言响应时。状态代码发生错误,告诉我没有安装带有标签的应用程序。我希望我的应用程序出现在已安装的应用程序设置中。
完整的错误消息:
(env) Inspiron-3793:~/Python-OC-Lettings-FR$ pytest lettings/
============================================================== test session starts ===============================================================
platform linux -- Python 3.9.7, pytest-7.1.1, pluggy-1.0.0 -- /home/arthur/openclass/P13/Python-OC-Lettings-FR/env/bin/python3
cachedir: .pytest_cache
django: settings: oc_lettings_site.settings (from ini)
rootdir: /home/arthur/openclass/P13/Python-OC-Lettings-FR, configfile: setup.cfg
plugins: django-3.9.0, mock-3.7.0
collected 1 item
lettings/tests/unit/tests.py::TestLettings::test_index ERROR [100%]
===================================================================== ERRORS =====================================================================
___________________________________________________ ERROR at setup of TestLettings.test_index ____________________________________________________
self = , app_label = 'oc_lettings_site'
def get_app_config(self, app_label):
"""
Import applications and returns an app config for the given label.
Raise LookupError if no application exists with this label.
"""
self.check_apps_ready()
try:
> return self.app_configs[app_label]
E KeyError: 'oc_lettings_site'
env/lib/python3.9/site-packages/django/apps/registry.py:155: KeyError
During handling of the above exception, another exception occurred:
request = >
@pytest.fixture(autouse=True)
def _django_db_marker(request):
"""Implement the django_db marker, internal to pytest-django.
This will dynamically request the ``db``, ``transactional_db`` or
``django_db_reset_sequences`` fixtures as required by the django_db marker.
"""
marker = request.node.get_closest_marker("django_db")
if marker:
transaction, reset_sequences = validate_django_db(marker)
if reset_sequences:
request.getfixturevalue("django_db_reset_sequences")
elif transaction:
request.getfixturevalue("transactional_db")
else:
> request.getfixturevalue("db")
env/lib/python3.9/site-packages/pytest_django/plugin.py:513:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
env/lib/python3.9/site-packages/pytest_django/fixtures.py:105: in django_db_setup
db_cfg = setup_databases(
env/lib/python3.9/site-packages/django/test/utils.py:169: in setup_databases
connection.creation.create_test_db(
env/lib/python3.9/site-packages/django/db/backends/base/creation.py:67: in create_test_db
call_command(
env/lib/python3.9/site-packages/django/core/management/__init__.py:168: in call_command
return command.execute(*args, **defaults)
env/lib/python3.9/site-packages/django/core/management/base.py:369: in execute
output = self.handle(*args, **options)
env/lib/python3.9/site-packages/django/core/management/base.py:83: in wrapped
res = handle_func(*args, **kwargs)
env/lib/python3.9/site-packages/django/core/management/commands/migrate.py:231: in handle
post_migrate_state = executor.migrate(
env/lib/python3.9/site-packages/django/db/migrations/executor.py:117: in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
env/lib/python3.9/site-packages/django/db/migrations/executor.py:147: in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
env/lib/python3.9/site-packages/django/db/migrations/executor.py:245: in apply_migration
state = migration.apply(state, schema_editor)
env/lib/python3.9/site-packages/django/db/migrations/migration.py:124: in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
env/lib/python3.9/site-packages/django/db/migrations/operations/special.py:190: in database_forwards
self.code(from_state.apps, schema_editor)
lettings/migrations/0002_auto_20220406_1825.py:13: in fill_address
Old_address = apps.get_model("oc_lettings_site", "address")
env/lib/python3.9/site-packages/django/apps/registry.py:205: in get_model
app_config = self.get_app_config(app_label)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = , app_label = 'oc_lettings_site'
def get_app_config(self, app_label):
"""
Import applications and returns an app config for the given label.
Raise LookupError if no application exists with this label.
"""
self.check_apps_ready()
try:
return self.app_configs[app_label]
except KeyError:
message = "No installed app with label '%s'." % app_label
for app_config in self.get_app_configs():
if app_config.name == app_label:
message += " Did you mean '%s'?" % app_config.label
break
> raise LookupError(message)
E LookupError: No installed app with label 'oc_lettings_site'.
env/lib/python3.9/site-packages/django/apps/registry.py:162: LookupError
------------------------------------------------------------- Captured stderr setup --------------------------------------------------------------
Creating test database for alias 'default'...
============================================================ short test summary info =============================================================
ERROR lettings/tests/unit/tests.py::TestLettings::test_index - LookupError: No installed app with label 'oc_lettings_site'.
================================================================ 1 error in 0.23s ================================================================
测试:
import pytest
from django.urls import reverse
@pytest.mark.django_db
class TestLettings:
'''This is the testclass for index_view'''
def test_index(self, client):
'''This test ...'''
response = client.get(reverse('lettings:index'))
assert response.status_code == 200
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报