開始使用免費開始

使用 pytest 測試程式碼

請看 test.py 中的以下測試程式碼。

def test_mortgage_calculator_example():
    '''
    Test an example case for the mortgage calculator
    '''

    # Instantiate a mortgage calculator
    mortgage_calculator = calculators.MortgageCalculator(200000,
                                                         0.065,
                                                         30)

    # Calculate the monthly payment
    monthly_payment = mortgage_calculator.monthly_payment

    # Run a test
    assert monthly_payment == 1264.14

如果用 pytest 檢查這個檔案,會通過嗎?請注意,在主控台使用 Pytest 時,需要在前面加上 !,例如 !pytest file_name.py,因為這是 IPython Shell。

本練習屬於課程

案例研究:用 Python 建構軟體

檢視課程

動手互動練習

將理論付諸實踐,立即體驗我們的互動練習

開始練習