Структура каталогов:
Код: Выделить всё
root/
├── subdir/
│ ├── mod.py
│ ├── other_mod.py
│ ├── __init__.py
| └── __tests__/
│ ├── test_mod.py
│ ├── __init__.py
├── __init__.py
└── app.py
Код: Выделить всё
#all __init__.py files are empty
#app.py
from subdir import mod
mod.function()
#subdir/mod.py
from .other_mod import other_function
def function():
return other_function()
#subdir/other_mod.py
def other_function():
return False
#subdir/__tests__/test_mod.py
from .. import mod
from unittest.mock import patch
def test_mod():
with patch('subdir.other_mod.other_function') as mock_other_function:
mock_other_function.return_value = True
assert mod.function()
Код: Выделить всё
$pytest subdir/__tests__/test_mod.py
Код: Выделить всё
FAILED subdir/__tests__/test_mod.py::test_mod - ValueError: Empty module name
Я изменил то, как я ссылаюсь наother_mod в вызове patch(), сделав его относительно разных мест, например тестового файла. или файл mod.py, но это приводит либо к той же ошибке, либо к ошибке «модуль не найден». Я также пробовал добавлять переменные name в различные файлы, но это не меняет проблему.
Что мне здесь не хватает? Как unittest хочет, чтобы я ссылался на модуль?
Подробнее здесь: https://stackoverflow.com/questions/791 ... and-pytest
Мобильная версия