我编写了兼容代码,尝试导入BufferedProtocol,并在引发ImportError时使用Protocol,但当我为这些代码编写测试时,我遇到了问题
我尝试使用模拟补丁asyncio,但它没有按预期工作。如果我将sys.modules中的asyncio模拟为None,它将无法检测是否使用了协议类,因为asyncio不是模拟上下文中的模块
我试过importlib,但似乎那个任务超出了我的能力
mymodule.py
try:
from asyncio import BufferedProtocol
class MyProtocol(BufferedProtocol):
pass
except ImportError:
from asyncio import Protocol
class MyProtocol(Protocol):
pass
test.py
def test_BufferedProtocol_not_exist(self):
with self.assertRaises(ImportError):
from asyncio import BufferedProtocol
with self.assertRaises(ImportError):
from asyncio import BufferedProtocol
import asyncio
self.assertFalse(hasattr(asyncio, 'BufferedProtocol'))
import asyncio.protocols
self.assertFalse(hasattr(asyncio.protocols, 'BufferedProtocol'))
from mymodule import MyProtocol
self.assertTrue(issubclass(MyProtocol, asyncio.Protocol))
我希望前面的测试通过Python3.7,并进行了一些模拟工作
目前没有回答
相关问题 更多 >
编程相关推荐