使用 xfail 處理失敗的測試
在這個練習中,你會第一次使用 pytest 標記來指定測試的行為。你已經看過函式 multiple_of_two,它用來檢查 num 是否為 2 的倍數。pytest 函式庫已經替你匯入。
本練習屬於課程
Python 測試入門
練習說明
- 為預期會失敗的測試加入正確的
pytest標記。 - 撰寫任一個預期會失敗的
assert測試。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def multiple_of_two(num):
if num == 0:
raise(ValueError)
return num % 2 == 0
# Add the pytest marker decorator here
____.____.____
def test_fails():
# Write any assert test that will fail
____ multiple_of_two(____) ____ ____