使用 doctest
doctest 模块提供了一种直接的方法,可用文档字符串中的示例来测试您的函数。在本练习中,您将有机会练习使用 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
____