伙計們,我想對一個基本上下載檔案的功能進行單元測驗,但我只是不知道如何去做。這是代碼:
from requests import get
from helpers.file_helper import get_file_path
def download_file(url, filename):
r = get(url)
filepath = get_file_path(filename)
with open(filepath, 'wb') as f:
f.write(r.content)
我想知道你們是如何解決這個問題的,如果可以的話,簡單地解釋一下,讓大家都能理解。
uj5u.com熱心網友回復:
您可以使用 pytest 的臨時檔案/目錄裝置來下載檔案、讀取和測驗輸出,并確保之后一切都干凈了。至于 API,如果您不想每次運行測驗時都呼叫它,則可以模擬它(使用pytest-mock或模擬包本身
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372145.html
