开始使用免费开始使用

使用 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 构建软件

查看课程

动手互动练习

通过我们的互动练习之一,将理论转化为实践

开始练习