コード例 #1
0
car2.setColour('White')
print('The colour is ' + car2.getColour())

# Similar functionality in mileage, move and engineSize except in this case Mileage is at 300 instead of the
# default value of 0.  This results in the mileage starting at 300 and becoming 320 after the move function.
# Unlike petrol and diesel cars which have fuel cylinders electric cars require fuel cells to run.  The function
# setNumberOfFuelCells takes in 2 values self and the value for fuel cells in this case 2.  The next line prints
# an appropriate message and uses the getNumberOfFuelCells which returns the value held in the variable which is 2.
car2.setMileage(300)
car2.move(20)
print('The mileage is ' + str(car2.getMileage())) + 'kms'
car2.engineSize = '2.1'
print 'The engine size is ' + car2.engineSize

car2.setNumberOfFuelCells(2)
print('The number of fuel cells is ' + str(car2.getNumberOfFuelCells()))


# A HybridCar Class is called by car3 allowing it to use the functions and variables of the class.  A hybrid is
# 1 part standard motor car and 1 part electric car so it will use a cylinder and a cell function.  The make
# takes in the value Toyota Prius and prints a message to the user.  The colour is set at blue and printed.
# Same functionality as other mileage in this case 100 is the value that is set, next 17 is added using move
# resulting in a value of 117kms.  The engineSize variable is set as 3.1.  Hybrid cars require both cells and
# cylinders so it will use both functions.
car3 = HybridCar()
car3.setMake('Toyota Prius')
print '\nOur hybrid option is ' + car3.getMake()

car3.setColour('Blue')
print('The colour is ' + car3.getColour())
コード例 #2
0
class TestElectricCar(unittest.TestCase):
    # The ElectricCar calls itself using self.
    def setUp(self):
        self.electric_car = ElectricCar()

    def test_electricCar_fuel_cells(self):
        # The default value for an electric car is called from the ElectricCar class which is 2 which is tested to
        #  make sure the base class ElectricCar functions are being correctly implemented.  The setNumberOfFuelCells
        # is used to change the default value from 2 to 8, this is tested and it has become 8 so is true.  5 and 7
        # are then placed as testing values for getNumberofFuelCells.  Note set doesn't change default values held
        # in base class.
        self.assertEqual(2, self.electric_car.getNumberOfFuelCells())
        self.electric_car.setNumberOfFuelCells(8)
        self.assertEqual(8, self.electric_car.getNumberOfFuelCells())
        self.electric_car.setNumberOfFuelCells(5)
        self.assertEqual(5, self.electric_car.getNumberOfFuelCells())
        self.electric_car.setNumberOfFuelCells(7)
        self.assertEqual(7, self.electric_car.getNumberOfFuelCells())

    def test_electricCar_mileage_and_move(self):
        # I am testing mileage and move for all cars to ensure correct implementation and because mileage is
        # an important factor for NCT tests and how many kilometers are on the clock.
        # As in the test_car_mileage 1st the default value is called this time from the ElectricCar class.
        # Move is then called using the value 15 is this is tested against the value retrieved from getMileage
        # which is 15 as well so it's true.  Mileage is then increased by 6.5 to 21.5.  Next the setMileage function
        # calls the function from the ElectricCar class which has inherited all the functions of the base class Car.
        # All the types of cars have inherited all the functions of Car.  In this case SetMileage is called and
        # takes the value 40.  This is tested using the getMileage function to ensure it's changed to 40 and it has.
        # The value 0 is tested and mileage variable remains 40.
        self.assertEqual(0, self.electric_car.getMileage())
        self.electric_car.move(15)
        self.assertEqual(15, self.electric_car.getMileage())
        self.electric_car.move(6.5)
        self.assertEqual(21.5, self.electric_car.getMileage())
        self.electric_car.setMileage(40)
        self.assertEqual(40, self.electric_car.getMileage())
        self.electric_car.move(0)
        self.assertEqual(40, self.electric_car.getMileage())

    def test_electricCar_make(self):
        # Even though make, engineSize, colour and paint are tested in the base class I feel it's important to
        # have test cases for the subclass' that implement the functions to ensure they are correctly inherited
        # with the same functionality so the default value ' ' is tested, a string with 1 word and a string with
        # 2 words.  Also i'm not using car.getMake but electric_car.getMake this will be the case in the other
        # subclass' where the functions are implemented from the subclass not the superclass/base class.
        self.assertEqual(' ', self.electric_car.getMake())
        self.electric_car.setMake('Charger')
        self.assertEqual('Charger', self.electric_car.getMake())
        self.electric_car.setMake('Super Charger')
        self.assertEqual('Super Charger', self.electric_car.getMake())

    def test_electricCar_engineSize(self):
        # Testing engineSize for ElectricCar class.
        self.assertEqual(' ', self.electric_car.getEngineSize())
        self.electric_car.setEngineSize(1.2)
        self.assertEqual(1.2, self.electric_car.getEngineSize())
        self.electric_car.setEngineSize(0.8)
        self.assertEqual(0.8, self.electric_car.getEngineSize())

    def test_electricCar_colour_and_paint(self):
        # Testing colour and paint for ElectricCar
        self.assertEqual(' ', self.electric_car.getColour())
        self.electric_car.paint('pink')
        self.assertEqual('pink', self.electric_car.getColour())
        self.electric_car.paint('orange')
        self.assertEqual('orange', self.electric_car.getColour())
        self.electric_car.setColour('yellow')
        self.assertEqual('yellow', self.electric_car.getColour())