將代碼封裝,并使用unittest調(diào)用時(shí),返回如下警告:
E:\intall\python-3.7.4-amd64\lib\unittest\suite.py:84: ResourceWarning: unclosed <socket.socket fd=228, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.206.1', 52676), raddr=('192.168.206.151', 8080)>
return self.run(*args, **kwds)
ResourceWarning: Enable tracemalloc to get the object allocation traceback
如下如圖所示:

解決辦法,可以通過warnings庫來忽略掉相關(guān)告警。
import warnings #導(dǎo)入warning模塊
warnings.simplefilter("ignore", ResourceWarning)

現(xiàn)在是unittest 調(diào)用,警告就不顯示出來啦!

浙公網(wǎng)安備 33010602011771號