【发布时间】:2019-06-18 07:33:32
【问题描述】:
我正在使用 Bleak 发现并连接到最近的蓝牙低功耗 (BLE) 设备,并且我目前正在编写单元测试(使用 pytest)。
我是 Python 测试的新手,我不知道如何处理这些补丁/模拟以使其适用于 async 函数。
我不知道是否应该使用实际功能,或者对默认功能应用补丁以使测试在没有 BLE 加密狗的情况下可执行。
这里是一个代码示例(discover.py的改进):
def list(op_sys: str) -> list:
"""list BLE devices
Returns:
list: status & list or error message
"""
import asyncio, platform
from bleak import discover
async def run() -> list:
"""discover BLE devices
Returns:
list: status & list or error message
"""
BLElist = []
try:
devices = await discover()
for d in devices:
print("'%s'" % d.name) # list devices
BLElist.append(d.name)
return 'success', BLElist
except:
return 'error', 'You don\'t have any BLE dongle.'
# linux = 3.6, windows = 3.7, need a new loop to work
if op_sys == "Windows":
asyncio.set_event_loop(asyncio.new_event_loop())
loop = asyncio.get_event_loop()
return loop.run_until_complete(run())
我想知道是否应该重写函数以将run() 部分移到外面,然后模拟它。
【问题讨论】:
标签: python unit-testing bluetooth-lowenergy pytest python-asyncio