Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import ClassVar
- class Vehicle:
- DEFAULT_FUEL_CONSUMPTION: ClassVar[float] = 1.25
- fuel_consumption: float
- fuel: float
- capacity: float
- horse_power: float
- def __init__(self, fuel: float, horse_power: float):
- self.fuel = fuel
- self.capacity = self.fuel
- self.horse_power = horse_power
- self.fuel_consumption = self.DEFAULT_FUEL_CONSUMPTION
- def drive(self, kilometers):
- fuel_needed = self.fuel_consumption * kilometers
- if self.fuel < fuel_needed:
- raise Exception("Not enough fuel")
- self.fuel -= fuel_needed
- def refuel(self, fuel):
- if self.fuel + fuel > self.capacity:
- raise Exception("Too much fuel")
- self.fuel += fuel
- def __str__(self):
- return f"The vehicle has {self.horse_power} " \
- f"horse power with {self.fuel} fuel left and {self.fuel_consumption} fuel consumption"
- import unittest
- class TestVehicle(unittest.TestCase):
- DEFAULT_FUEL_CONSUMPTION = 1.25
- def setUp(self):
- self.vehicle = Vehicle(100, 150)
- def test_initialization_is_correct(self):
- self.assertEqual(100, self.vehicle.fuel)
- self.assertEqual(100, self.vehicle.capacity)
- self.assertEqual(150, self.vehicle.horse_power)
- self.assertEqual(1.25, self.vehicle.fuel_consumption)
- def test_raise_exception_if_not_enough_fuel(self):
- with self.assertRaises(Exception):
- self.vehicle.drive(100)
- self.assertEqual(100, self.vehicle.fuel)
- def test_drive_reduces_fuel_when_distance_is_reachable(self):
- distance = 50
- fuel_needed = self.DEFAULT_FUEL_CONSUMPTION * distance # необходимото гориво за да се изминат 50 км
- self.vehicle.drive(distance) # извикваме метода drive и подаваме километрите
- expected_result = 100 - fuel_needed # горивото, което сме имали в началото - нужното гориво за изминаване на 50 км
- self.assertEqual(expected_result, self.vehicle.fuel)
- # прави test и ако е равно на нула
- def test_if_fuel_is_over_capacity_raise_exeption(self):
- with self.assertRaises(Exception):
- self.vehicle.refuel(10)
- def test_added_fuel_is_less_capacity(self):
- self.vehicle.fuel -= 20
- self.vehicle.refuel(20)
- self.assertEqual(100, self.vehicle.fuel)
- def test_vehicle_str_returns_proper_sting_message(self):
- actual_result = str(self.vehicle)
- expected = f"The vehicle has 150 " \
- f"horse power with 100 fuel left and 1.25 fuel consumption"
- if __name__ == '__main__':
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement