python-3.x pytest-django找不到我现有的模块

y4ekin9u  于 2023-11-20  发布在  Python
关注(0)|答案(1)|浏览(132)

为了给予一点上下文,我正在学习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中的软件包)

bnl4lu3b

bnl4lu3b1#

成功地做到了:

  • 我将项目结构从
django-pytest
\_ django_pytest
  \_ django_pytest
  |_ library
  |_ tests
  |_ __init__.py
  |_ manage.py

字符串

django-pytest
\_ project_tets
  \_ django_pytest
  |_ library
  |_ tests
  |_ __init__.py
  |_ manage.py

  • 我将测试中的导入从
from project_test.library.models import Book


from library.models import Book


这些修改对我来说似乎不多,我不明白为什么他们解决了我的问题。
Django创建了两个同名的包的结构,所以我想我必须系统地重命名父包才能让pytest-django工作。
并承担这些linted进口X)

相关问题