[英]Test for import of optional dependencies in __init__.py with pytest: Python 3.5 /3.6 differs in behaviour
我有一個用於 python 3.5 和 3.6 的包,它具有可選的依賴項,我想要在任一版本上運行的測試 (pytest)。
我在下面做了一個包含兩個文件的簡化示例,一個簡單的__init__.py
,其中導入了可選包“requests”(只是一個示例),並設置了一個標志來指示請求的可用性。
mypackage/
├── mypackage
│ └── __init__.py
└── test_init.py
__init__.py
文件內容:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
requests_available = True
try:
import requests
except ImportError:
requests_available = False
test_init.py
文件內容:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest, sys
def test_requests_missing(monkeypatch):
import mypackage
import copy
fakesysmodules = copy.copy(sys.modules)
fakesysmodules["requests"] = None
monkeypatch.delitem(sys.modules,"requests")
monkeypatch.setattr("sys.modules", fakesysmodules)
from importlib import reload
reload(mypackage)
assert mypackage.requests_available == False
if __name__ == '__main__':
pytest.main([__file__, "-vv", "-s"])
test_requests_missing
測試適用於 Python 3.6.5:
runfile('/home/bjorn/python_packages/mypackage/test_init.py', wdir='/home/bjorn/python_packages/mypackage')
============================= test session starts ==============================
platform linux -- Python 3.6.5, pytest-3.6.1, py-1.5.2, pluggy-0.6.0 -- /home/bjorn/anaconda3/envs/bjorn36/bin/python
cachedir: .pytest_cache
rootdir: /home/bjorn/python_packages/mypackage, inifile:
plugins: requests-mock-1.5.0, mock-1.10.0, cov-2.5.1, nbval-0.9.0, hypothesis-3.38.5
collecting ... collected 1 item
test_init.py::test_requests_missing PASSED
=========================== 1 passed in 0.02 seconds ===========================
但不是在 Python 3.5.4 上:
runfile('/home/bjorn/python_packages/mypackage/test_init.py', wdir='/home/bjorn/python_packages/mypackage')
========================================================= test session starts ==========================================================
platform linux -- Python 3.5.4, pytest-3.6.1, py-1.5.2, pluggy-0.6.0 -- /home/bjorn/anaconda3/envs/bjorn35/bin/python
cachedir: .pytest_cache
rootdir: /home/bjorn/python_packages/mypackage, inifile:
plugins: requests-mock-1.5.0, mock-1.10.0, cov-2.5.1, nbval-0.9.1, hypothesis-3.38.5
collecting ... collected 1 item
test_init.py::test_requests_missing FAILED
=============================================================== FAILURES ===============================================================
________________________________________________________ test_requests_missing _________________________________________________________
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f9a2953acc0>
def test_requests_missing(monkeypatch):
import mypackage
import copy
fakesysmodules = copy.copy(sys.modules)
fakesysmodules["requests"] = None
monkeypatch.delitem(sys.modules,"requests")
monkeypatch.setattr("sys.modules", fakesysmodules)
from importlib import reload
> reload(mypackage)
test_init.py:13:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../anaconda3/envs/bjorn35/lib/python3.5/importlib/__init__.py:166: in reload
_bootstrap._exec(spec, module)
<frozen importlib._bootstrap>:626: in _exec
???
<frozen importlib._bootstrap_external>:697: in exec_module
???
<frozen importlib._bootstrap>:222: in _call_with_frames_removed
???
mypackage/__init__.py:8: in <module>
import requests
../../anaconda3/envs/bjorn35/lib/python3.5/site-packages/requests/__init__.py:97: in <module>
from . import utils
.... VERY LONG OUTPUT ....
from . import utils
../../anaconda3/envs/bjorn35/lib/python3.5/site-packages/requests/__init__.py:97: in <module>
from . import utils
<frozen importlib._bootstrap>:968: in _find_and_load
???
<frozen importlib._bootstrap>:953: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:896: in _find_spec
???
<frozen importlib._bootstrap_external>:1171: in find_spec
???
<frozen importlib._bootstrap_external>:1145: in _get_spec
???
<frozen importlib._bootstrap_external>:1273: in find_spec
???
<frozen importlib._bootstrap_external>:1245: in _get_spec
???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
name = 'requests', location = '/home/bjorn/anaconda3/envs/bjorn35/lib/python3.5/site-packages/requests/__init__.py'
> ???
E RecursionError: maximum recursion depth exceeded
<frozen importlib._bootstrap_external>:575: RecursionError
======================================================= 1 failed in 2.01 seconds =======================================================
我有兩個問題:
為什么我會看到這種差異? 相關軟件包在 3.5 和 3.6 上的版本似乎相同。
有沒有更好的方法來做我想做的事? 我現在的代碼是從網上找到的例子拼接在一起的。 我試圖修補導入機制以避免“重新加載”,但我沒有成功。
我要么模擬__import__
函數(在import modname
語句后面調用的那個),要么通過添加自定義元路徑查找器來自定義導入機制。 例子:
sys.meta_path
添加一個自定義MetaPathFinder
實現,該實現在嘗試導入pkgnames
任何包時引發ImportError
:
class PackageDiscarder:
def __init__(self):
self.pkgnames = []
def find_spec(self, fullname, path, target=None):
if fullname in self.pkgnames:
raise ImportError()
@pytest.fixture
def no_requests():
sys.modules.pop('requests', None)
d = PackageDiscarder()
d.pkgnames.append('requests')
sys.meta_path.insert(0, d)
yield
sys.meta_path.remove(d)
@pytest.fixture(autouse=True)
def cleanup_imports():
yield
sys.modules.pop('mypackage', None)
def test_requests_available():
import mypackage
assert mypackage.requests_available
@pytest.mark.usefixtures('no_requests2')
def test_requests_missing():
import mypackage
assert not mypackage.requests_available
夾具no_requests
將在調用時更改sys.meta_path
,因此自定義元路徑查找器從可以導入的包中過濾掉requests
包名稱(我們不能在任何導入時引發,否則pytest
本身會中斷)。 cleanup_imports
只是為了確保在每次測試中都會重新導入mypackage
。
__import__
import builtins
import sys
import pytest
@pytest.fixture
def no_requests(monkeypatch):
import_orig = builtins.__import__
def mocked_import(name, globals, locals, fromlist, level):
if name == 'requests':
raise ImportError()
return import_orig(name, locals, fromlist, level)
monkeypatch.setattr(builtins, '__import__', mocked_import)
@pytest.fixture(autouse=True)
def cleanup_imports():
yield
sys.modules.pop('mypackage', None)
def test_requests_available():
import mypackage
assert mypackage.requests_available
@pytest.mark.usefixtures('no_requests')
def test_requests_missing():
import mypackage
assert not mypackage.requests_available
在這里,fixture no_requests
負責用一個會在import requests
嘗試時引發的函數替換__import__
函數,在其余的導入上做得很好。
import sys
from unittest.mock import patch
def test_without_dependency(self):
with patch.dict(sys.modules, {'optional_dependency': None}):
# do whatever you want
上面的代碼所做的是,它模擬了包optional_dependency
沒有安裝並在上下文管理器( with
) 內的隔離環境中運行您的測試。
請記住,您可能需要根據您的用例重新加載被測module
import sys
from unittest.mock import patch
from importlib import reload
def test_without_dependency(self):
with patch.dict(sys.modules, {'optional_dependency': None}):
reload(sys.modules['my_module_under_test'])
# do whatever you want
如果測試測試可選功能,則在缺少該功能時應跳過它而不是通過測試。
test.support.import_module()
是 Python 自動測試套件中用於在模塊丟失時跳過測試或測試文件的函數:
import test.support
import unittest
nonexistent = test.support.import_module("nonexistent")
class TestDummy(unittest.testCase):
def test_dummy():
self.assertTrue(nonexistent.vaporware())
然后,在運行時:
> python -m py.test -rs t.py
<...>
collected 0 items / 1 skipped
=========================== short test summary info ===========================
SKIP [1] C:\Python27\lib\test\support\__init__.py:90: SkipTest: No module named
nonexistent
========================== 1 skipped in 0.05 seconds ==========================
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.