Python编程之doctest小练习

hxy    2020-02-20 14:10

        有这样一道Python编程的练习题,挺有意思,随手做个记录。

Write a class definition for a Car class that contains information about the average kilometres per litre that it can achieve and the volume of its petrol tank.
 
Write an instance method that returns the typical/standard distance the car can be driven on a full tank. Include a doctest that tests a sample input.
 
In your main routine create two instances of the class and print the typical range for each. The first car should typically average 20 kpl (kilometres per litre)  and have a tank volume of 100 litres.
 
The second car should average 24 kpl and have a tank volume of 80 liters.

        主要要求就是编写一个Car类,包含计算汽车行驶里程的一个方法,并且通过doctest测试。一般在写函数的时候,就是手动测试一下就行了,连注释写的都不规范。这个练习题让我了解了doctest的文档测试。
示例如下:
# -*- coding: utf-8 -*-
'''
@Descripttion: Python programing practice -- doctest
@Version: 2.0
@Author: 
@Date: 2020-02-20 13:12:48
@LastEditors: 
@LastEditTime: 2020-02-20 14:57:08
'''
class Car:

    def __init__(self, aveKilos=None, volume=None):
        '''
        @description: A class definition for a Car class that contains information about
            the average kilometres per litre that it can achieve and the volume
            of its petrol tank.
        @param : 
            aveKilos: average kilometres per litre; 
            volume: the volume of its petrol tank
        @return: 

        '''
        self.aveKilos = aveKilos
        self.volume = volume
        info = 'aveKilos:{} volume:{}'.format(aveKilos, volume)
        print(info)


    def distanceDrive(self):
        '''
        @description: 
            An instance method that returns the typical/standard distance the car can be driven on a full tank. 
        @return: 
            The typical/standard distance the car can be driven on a full tank. 
        '''
        total = self.aveKilos * self.volume
        return total
    

    def test(self):
        '''
        @description: A doctest function that tests a sample input.
        >>> c1 = Car(20, 100)
        aveKilos:20 volume:100
        >>> c2 = Car(24, 80)
        aveKilos:24 volume:80
        >>> c1.distanceDrive()
        2000
        >>> c2.distanceDrive()
        1920
        '''
        pass


if __name__ == '__main__':
    '''
        Main routine: create two instances of the class and print the typical range for each. 
        - The first car should typically average 20 kpl (kilometres per litre)  and 
            have a tank volume of 100 litres.
        - The second car should average 24 kpl and have a tank volume of 80 liters.
    '''
    import doctest
    doctest.testmod(verbose=True)
    
    c1 = Car(20, 100)
    print(c1.distanceDrive())

    c2 = Car(24, 80)
    print(c2.distanceDrive())
执行测试的结果是:
Trying:
    c1 = Car(20, 100)
Expecting:
    aveKilos:20 volume:100
ok
Trying:
    c2 = Car(24, 80)
Expecting:
    aveKilos:24 volume:80
ok
Trying:
    c1.distanceDrive()
Expecting:
    2000
ok
Trying:
    c2.distanceDrive()
Expecting:
    1920
ok
4 items had no tests:
    __main__
    __main__.Car
    __main__.Car.__init__
    __main__.Car.distanceDrive
1 items passed all tests:
   4 tests in __main__.Car.test
4 tests in 5 items.
4 passed and 0 failed.
Test passed.
aveKilos:20 volume:100
2000
aveKilos:24 volume:80
1920
Last Modified: 2020-02-20 14:57
Views: 1.5K

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment