为了给予一点上下文,我正在学习django中的test unit,并制作了一个非常简单的应用程序(Here is the structure of my project)。
我的问题是:
当在我的django应用上启动单元测试时,我得到了一个ModuleNotFoundError事件,尽管模块存在。
代码示例:
from django.shortcuts import get_object_or_404, render
from .models import Book
def book_infos(request, pk):
book = get_object_or_404(Book, pk=pk)
return render(request, "book_infos.html", {'book': book})
字符串
和相关的测试:
import pytest
from django.urls import reverse
from django.test import Client
from django_pytest.library.models import Book
from pytest_django.asserts import assertTemplateUsed
@pytest.mark.django_db
def test_book_infos_view():
client = Client()
Book.objects.create(author="Jules Verne",
title="20 milles lieues sous les mers")
path = reverse('infos', kwargs={'pk': 1})
response = client.get(path)
content = response.content.decode()
expected_content = "<p>Jules Verne | 20 milles lieues sous les mers</p>"
assert content == expected_content
assert response.status_code == 200
assertTemplateUsed(response, "book_infos.html")
型
我的pytest.ini如下
[pytest]
pythonpath = . library tests
DJANGO_SETTINGS_MODULE = django_pytest.settings
python_files = test_*.py
型
当我从django-pytest运行pytest tests/library/test_views.py
时,我得到了这样的结果:
platform win32 -- Python 3.11.5, pytest-7.4.3, pluggy-1.3.0
django: version: 4.2.7, settings: django_pytest.settings (from ini)
rootdir: ...\django-pytest\django_pytest
configfile: pytest.ini
plugins: django-4.6.0
_ ERROR collecting tests/library/test_views.py _
ImportError while importing test module '...\django-pytest\django_pytest\tests\library\test_views.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Python311\Lib\importlib\__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
E ModuleNotFoundError: No module named 'django_pytest.tests'
型
在debug中运行相同的测试,我得到了这个:
ERROR: not found: ...\django-pytest\django_pytest\tests\library\test_views.py::test_book_infos_view
(no name '...\\django-pytest\\django_pytest\\tests\\library\\test_views.py::test_book_infos_view' in any of [<Module test_views.py>])
tests/library/test_views.py:None (tests/library/test_views.py)
ImportError while importing test module '...\django-pytest\django_pytest\tests\library\test_views.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\..\..\venv\Lib\site-packages\_pytest\python.py:617: in _importtestmodule
mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
..\..\..\venv\Lib\site-packages\_pytest\pathlib.py:567: in import_path
importlib.import_module(module_name)
C:\Python311\Lib\importlib\__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
E ModuleNotFoundError: No module named 'django_pytest.tests'
型
我所尝试的:
我检查了pytest/pytest-django的兼容性。
我将pythonpath = . library tests
添加到pytest.ini following this中。
我尝试从django-pytest
而不是django-pytest/django_pytest
启动py -m pytest
,但得到以下ERROR django_pytest/tests/library/test_views.py - 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 accessin...
我尝试改变项目结构(将测试移到库模块中)。
有人知道为什么会出现这个错误吗?
谢谢你,谢谢
堆栈:
Python==3.11
Django==4.2.7
pytest==7.4.3
pytest-django==4.6.0
型
(All安装在venv中的软件包)
1条答案
按热度按时间bnl4lu3b1#
成功地做到了:
字符串
到
型
型
到
型
这些修改对我来说似乎不多,我不明白为什么他们解决了我的问题。
Django创建了两个同名的包的结构,所以我想我必须系统地重命名父包才能让pytest-django工作。
并承担这些linted进口X)