Advertisement
horozov86

vehicle_testing_directly_after_basic_class!!!

Jul 26th, 2023
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. from typing import ClassVar
  2.  
  3.  
  4. class Vehicle:
  5.     DEFAULT_FUEL_CONSUMPTION: ClassVar[float] = 1.25
  6.     fuel_consumption: float
  7.     fuel: float
  8.     capacity: float
  9.     horse_power: float
  10.  
  11.     def __init__(self, fuel: float, horse_power: float):
  12.         self.fuel = fuel
  13.         self.capacity = self.fuel
  14.         self.horse_power = horse_power
  15.         self.fuel_consumption = self.DEFAULT_FUEL_CONSUMPTION
  16.  
  17.     def drive(self, kilometers):
  18.         fuel_needed = self.fuel_consumption * kilometers
  19.         if self.fuel < fuel_needed:
  20.             raise Exception("Not enough fuel")
  21.         self.fuel -= fuel_needed
  22.  
  23.     def refuel(self, fuel):
  24.         if self.fuel + fuel > self.capacity:
  25.             raise Exception("Too much fuel")
  26.         self.fuel += fuel
  27.  
  28.     def __str__(self):
  29.         return f"The vehicle has {self.horse_power} " \
  30.                f"horse power with {self.fuel} fuel left and {self.fuel_consumption} fuel consumption"
  31.                
  32. import unittest
  33.  
  34. class TestVehicle(unittest.TestCase):
  35.     DEFAULT_FUEL_CONSUMPTION = 1.25
  36.     def setUp(self):
  37.         self.vehicle = Vehicle(100, 150)
  38.        
  39.     def test_initialization_is_correct(self):
  40.         self.assertEqual(100, self.vehicle.fuel)
  41.         self.assertEqual(100, self.vehicle.capacity)
  42.         self.assertEqual(150, self.vehicle.horse_power)
  43.         self.assertEqual(1.25, self.vehicle.fuel_consumption)
  44.        
  45.     def test_raise_exception_if_not_enough_fuel(self):
  46.         with self.assertRaises(Exception):
  47.             self.vehicle.drive(100)
  48.         self.assertEqual(100, self.vehicle.fuel)
  49.        
  50.     def test_drive_reduces_fuel_when_distance_is_reachable(self):
  51.         distance = 50
  52.         fuel_needed = self.DEFAULT_FUEL_CONSUMPTION * distance # необходимото гориво за да се изминат 50 км
  53.         self.vehicle.drive(distance) # извикваме метода drive и подаваме километрите
  54.         expected_result = 100 - fuel_needed # горивото, което сме имали в началото - нужното гориво за изминаване на 50 км
  55.         self.assertEqual(expected_result, self.vehicle.fuel)
  56.    
  57.     # прави test и ако е равно на нула
  58.        
  59.     def test_if_fuel_is_over_capacity_raise_exeption(self):
  60.         with self.assertRaises(Exception):
  61.             self.vehicle.refuel(10)
  62.            
  63.     def test_added_fuel_is_less_capacity(self):
  64.         self.vehicle.fuel -= 20
  65.         self.vehicle.refuel(20)
  66.         self.assertEqual(100, self.vehicle.fuel)
  67.        
  68.     def test_vehicle_str_returns_proper_sting_message(self):
  69.         actual_result = str(self.vehicle)
  70.         expected = f"The vehicle has 150 " \
  71.                     f"horse power with 100 fuel left and 1.25 fuel consumption"
  72.        
  73.        
  74. if __name__ == '__main__':
  75.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement