Структура проекта: < /p>
└──
├── __init__.py
├── celery_worker.py
├── Dockerfile
├── main.py
├──
│ ├── middlewares.py
├── requirements.txt
├──
│ ├── process.py
├──
│ ├── test_process.py
< /code>
Я запускаю pytest из каталога приложений: < /p>
(venv) PS C:\Users\Administrador\Desktop\code\repo\fastapi_celery_redis\app> pytest
< /code>
Но я получаю следующую ошибку: < /p>
================================================= test session starts =================================================
platform win32 -- Python 3.13.2, pytest-8.3.4, pluggy-1.5.0
rootdir: C:\Users\Administrador\Desktop\code\repo\fastapi_celery_redis\app
plugins: anyio-4.8.0
collected 0 items / 1 error
======================================================= ERRORS ========================================================
_________________________________ ERROR collecting fastapi_app/tests/test_process.py __________________________________
ImportError while importing test module 'C:\Users\Administrador\Desktop\code\repo\fastapi_celery_redis\app\fastapi_app\tests\test_process.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\..\..\..\..\AppData\Local\Programs\Python\Python313\Lib\importlib\__init__.py:88: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
fastapi_app\tests\test_process.py:3: in
from ..main import app
E ImportError: attempted relative import with no known parent package
=============================================== short test summary info ===============================================
ERROR fastapi_app/tests/test_process.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================== 1 error in 0.90s ===================================================
< /code>
test_process.py Content: < /p>
import pytest
from fastapi.testclient import TestClient
from ..main import app
# Cliente de pruebas
client = TestClient(app)
def test_add_task_success():
"""Prueba cuando la tarea se envía exitosamente"""
data = {"number": 10, "text": "Test task"}
response = client.post("/add-task", json=data)
assert response.status_code == 200
result = response.json()
assert result["error"] is False
assert result["message"] == "Tarea enviada exitosamente"
assert result["result"]["message"] == "Tarea enviada"
< /code>
Что я пробовал до сих пор: < /p>
Запуск pytest как из App, так и из каталогов FASTAPI_APP. < /li>
Изменение импорта в test_process.py на абсолютный импорт < /li>
< /ol>
from fastapi_app.main import app
< /code>
Ничто из них не решило проблему. Как я могу правильно импортировать приложение в своем тестовом файле и сделать Pytest распознавать мою структуру пакета?
Подробнее здесь: https://stackoverflow.com/questions/794 ... own-parent