開始使用免費開始

使用 doctest

doctest 模組提供一個簡單的方法,讓你用 docstring 裡的範例來測試你的函式。在這個練習中,你會實作並練習使用 doctest 模組進行測試。

doctest 模組已經預先載入到你的環境中。

本練習屬於課程

案例研究:用 Python 建構軟體

檢視課程

練習說明

  • FinancialCalculator 類別中,計算月利率。也就是年利率除以一年中的月份數(12)。
  • 使用 doctest 模組中對應的函式,來測試你在函式範例碼中的行為。

請記住,只有當程式碼或文件不正確時,doctest 才會輸出訊息。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

class FinancialCalculator(BasicCalculator):
    def monthly_interest(self, annual_interest_rate):
      '''
      >>> monthly_interest(0.06)
      0.005
      '''
      # Calculate the quotient of the annual_interest_rate and 12 (the number of months in a year).
      return self.divide(____, ____)

# Run doctest
____
編輯並執行程式碼