使用 fixtures 進行資料管線的單元測試
你在上一支影片中學到,單元測試可以讓資料管線更值得信任,甚至能在開發過程中提早抓出臭蟲。這個練習中,你會使用 pytest 函式庫與 assert,實作 fixtures 與單元測試。
下面提供 transform 函式作為參考,你將以它為核心撰寫單元測試。pandas 已以 pd 名稱匯入,pytest() 函式庫也已載入並可使用。
def transform(raw_data):
raw_data["tax_rate"] = raw_data["total_taxes_paid"] / raw_data["total_taxable_income"]
raw_data.set_index("industry_name", inplace=True)
return raw_data
本練習屬於課程
使用 Python 的 ETL 與 ELT
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define a pytest fixture
@pytest.fixture()
____ ____():
raw_data = pd.read_csv("raw_tax_data.csv")
# Transform the raw_data, store in clean_data DataFrame, and return the variable
clean_data = ____
return ____