ฉันใช้ Bleak เพื่อค้นหาและเชื่อมต่อกับอุปกรณ์ Bluetooth Low Energy (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()
ออกไปข้างนอกและเยาะเย้ยมันหรือไม่