class TestPetrolCar(unittest.TestCase):

    def setUp(self):
        # The PetrolCar class calls itself.
        self.petrol_car = PetrolCar()

    def test_PetrolCar_fuel_cylinders(self):
        # Testing a PetrolCar the default value of 3 is called from the base class Petrol Car and is tested to
        # ensure the base class PetrolCar functions are being correctly implemented.  The setNumberOfFuelCylinders
        # in this case changes the default value from 3 to 7 this is tested and it has become 7 so is true.
        # This is similar to ElectricCar FuelCells implementation but with cylinders instead of cells.
        # The cylinders are tested with values for 2 and 5 which pass.
        self.assertEqual(3, self.petrol_car.getNumberOfFuelCylinders())
        self.petrol_car.setNumberOfFuelCylinders(7)
        self.assertEqual(7, self.petrol_car.getNumberOfFuelCylinders())
        self.petrol_car.setNumberOfFuelCylinders(2)
        self.assertEqual(2, self.petrol_car.getNumberOfFuelCylinders())
        self.petrol_car.setNumberOfFuelCylinders(5)
        self.assertEqual(5, self.petrol_car.getNumberOfFuelCylinders())

    def test_PetrolCar_mileage_and_move(self):
        # Ths function tests the mileage of the PetrolCar class.  As in the other tests it starts with the default
        # value from the base class.  Move is then called using the value 5 this is tested against the value
        # retrieved from getMileage which is now increased to 5 so it's true.  It is increased by 12 to make 17,
        # tested by adding 0 still gives 17, tested adding a decimal 6.5 to get 23.5.
        self.assertEqual(0, self.petrol_car.getMileage())
        self.petrol_car.move(5)
        self.assertEqual(5, self.petrol_car.getMileage())
        self.petrol_car.move(12)
        self.assertEqual(17, self.petrol_car.getMileage())
        self.petrol_car.move(0)
        self.assertEqual(17, self.petrol_car.getMileage())
        self.petrol_car.move(6.5)
        self.assertEqual(23.5, self.petrol_car.getMileage())

    def test_PetrolCar_make(self):
        # Testing make function for PetrolCar to ensure correctly inherited from base class Car.
        self.assertEqual(' ', self.petrol_car.getMake())
        self.petrol_car.setMake('Ford')
        self.assertEqual('Ford', self.petrol_car.getMake())
        self.petrol_car.setMake('Ford Focus')
        self.assertEqual('Ford Focus', self.petrol_car.getMake())

    def test_PetrolCar_engineSize(self):
        # Testing engineSize for PetrolCar class.
        self.assertEqual(' ', self.petrol_car.getEngineSize())
        self.petrol_car.setEngineSize(2.2)
        self.assertEqual(2.2, self.petrol_car.getEngineSize())
        self.petrol_car.setEngineSize(2.6)
        self.assertEqual(2.6, self.petrol_car.getEngineSize())

    def test_PetrolCar_colour_and_paint(self):
        # Testing colour and paint for PetrolCar Class
        self.assertEqual(' ', self.petrol_car.getColour())
        self.petrol_car.paint('black')
        self.assertEqual('black', self.petrol_car.getColour())
        self.petrol_car.setColour('dark blue')
        self.assertEqual('dark blue', self.petrol_car.getColour())
Exemplo n.º 2
0
class TestPetrol(unittest.TestCase):
    
    def setUp(self):
        self.petrol = PetrolCar()
    
    def test_petrol_engineSize(self):
        self.assertEqual('', self.petrol.getEngineSize())
        self.petrol.setEngineSize('1.5')
        self.assertEqual('1.5', self.petrol.getEngineSize())
class TestCar(unittest.TestCase):
    def setUp(self):
        self.car = Car()
        self.car1 = ElectricCar(1)
        self.car2 = PetrolCar(2)
        self.car3 = DieselCar(3)
        self.car4 = HybridCar(4)
        self.dealership = Dealership()
        self.dealership.create_current_stock()

    def test_car_mileage(self):
        self.assertEqual(0, self.car.getMileage())
        self.car.move(15)  # this function exists for all child classes of car
        self.assertEqual(15, self.car.getMileage())

    def test_car_make(self):
        self.car.setMake(
            'Ferrari')  # this function exists for all child classes of car
        self.assertEqual('Ferrari', self.car.getMake())
        self.assertEqual('Ferrari', self.car.getMake())
        self.assertEqual('Tesla', self.car1.getMake())
        self.assertEqual('Toyota', self.car2.getMake())
        self.assertEqual('Volkswagon', self.car3.getMake())
        self.assertEqual('Mitsubishi', self.car4.getMake())

    def test_car_colour(self):
        self.assertEqual("Aungier Rental Red - Branded", self.car.getColour())
        self.car.paint(
            'red')  # this function exists for all child classes of car
        self.assertEqual('red', self.car.getColour())

    def test_car_engine(self):
        self.assertEqual('85-kWh', self.car1.getEngineSize())
        self.assertEqual('1.6 ltr', self.car2.getEngineSize())
        self.assertEqual('1.9 ltr', self.car3.getEngineSize())
        self.assertEqual('2.0 ltr', self.car4.getEngineSize())

    def test_rentalprocess(
        self
    ):  # test the rental process by renting out one car and checking it back in. verifies stock levels also
        self.assertEqual(0, len(self.dealership.rented_cars))
        self.assertEqual(8, len(self.dealership.hybrid_cars))
        self.dealership.rent(self.dealership.hybrid_cars, 1, "Test")
        self.assertEqual(1, len(self.dealership.rented_cars))
        self.assertEqual("Test", self.dealership.rented_cars[0][1])
        self.assertEqual(7, len(self.dealership.hybrid_cars))

        original_raw_input = __builtins__.raw_input
        __builtins__.raw_input = lambda _: "135"
        self.assertEqual(self.dealership.rented("Test"), None)
        __builtins__.raw_input = original_raw_input

        self.assertEqual(0, len(self.dealership.rented_cars))
        self.assertEqual(8, len(self.dealership.hybrid_cars))