У меня есть следующая упрощенная структура проекта Python 2.7:
project/
├── libs/
| └── zipfile.py
├── tests/
| ├── __init__.py
| └── test_hello.py
├── hello.py
└── main.py
Я хочу, чтобы этот проект использовал исправленную версию одного из встроенных модулей Python (который в этом примере zipfile
), расположенный в libs
. Обратите внимание, что это внешнее требование, и я не могу изменить структуру проекта.
Ниже приведена упрощенная реализация каждого файла:
ЛИЭС / zipfile.py
def is_zipfile(filename):
return "Patched zipfile called"
Тесты / test_hello.py
from hello import hello
def test_hello():
assert hello() == "Patched zipfile called"
hello.py
import os
import sys
libs_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "libs"))
if libs_path not in sys.path:
sys.path.insert(1, libs_path)
import zipfile
def hello():
print(zipfile.__file__) # to check which zipfile module is imported
result = zipfile.is_zipfile("some_path")
return result
main.py
from hello import hello
def main():
print(hello())
if __name__ == "__main__":
main()
При непосредственном запуске программы (python main.py
) я получил ожидаемый результат:
/home/project/libs/zipfile.pyc
Patched zipfile called
Однако при запуске pytest с project
в качестве рабочего каталога (pytest -s
) произошел сбой:
/usr/lib/python2.7/zipfile.pyc
================================== FAILURES ===================================
_________________________________ test_hello __________________________________
def test_hello():
> assert hello() == "Patched zipfile called"
E assert False == 'Patched zipfile called'
E + where False = hello()
tests/test_hello.py:4: AssertionError
========================== 1 failed in 0.13 seconds ===========================
Я пробовал пару решений, представленных в этом посте SO , например, запуск python -m pytest
, но ни одно из них не помогло мне. Есть ли способ успешно запустить этот тест не хакерским способом?