class TestingVehicle(unittest.TestCase): def setUp(self): self.vehicle = Vehicle(20.00, 150.00) def test_if_init_is_properly_instantiated(self): self.assertEqual(self.vehicle.fuel, 20.0) self.assertEqual(self.vehicle.horse_power, 150.00) def test_drive_method_if_fuel_gets_reduced(self): self.vehicle.drive(10) self.assertEqual(self.vehicle.fuel, 7.5) def test_drive_method_if_fuel_is_enough(self): with self.assertRaises(Exception) as context_manager: self.vehicle.drive(100000000) self.assertEqual(str(context_manager.exception), 'Not enough fuel') def test_refuel_method_if_fuel_gets_increased(self): self.vehicle.drive(10) self.vehicle.refuel(5) self.assertEqual(self.vehicle.fuel, 12.5) def test_refuel_if_fuel_is_too_much(self): with self.assertRaises(Exception) as context_manager: self.vehicle.refuel(100000000) self.assertEqual(str(context_manager.exception), 'Too much fuel') def test_if_str_method_returns_the_correct_string(self): expected = 'The vehicle has 150.0 horse power' \ ' with 20.0 fuel left and 1.25 fuel consumption' actual = self.vehicle.__str__() self.assertEqual(actual, expected)
class TestVehicle(unittest.TestCase): def setUp(self): self.car = Vehicle(100, 5) def test_attrib_assigned(self): self.assertEqual(self.car.fuel, 100) self.assertEqual(self.car.capacity, 100) self.assertEqual(self.car.horse_power, 5) self.assertEqual(self.car.fuel_consumption, 1.25) def test_drive_ok(self): self.car.drive(10) self.assertEqual(self.car.fuel, 87.5) def test_drive_not_ok(self): with self.assertRaises(Exception) as ex: self.car.drive(100) self.assertEqual(str(ex.exception), 'Not enough fuel') def test_refuel_is_ok(self): self.car.drive(5) self.car.refuel(5) self.assertEqual(self.car.fuel, 98.75) def test_refuel_is_not_ok_should_raise_exception(self): self.car.drive(5) with self.assertRaises(Exception) as ex: self.car.refuel(15) self.assertEqual(str(ex.exception), "Too much fuel") def test_string_method(self): self.assertEqual(self.car.__str__(), 'The vehicle has 5 horse power with 100 fuel left and 1.25 fuel consumption')
class VehicleTests(unittest.TestCase): def setUp(self): self.vehicle = Vehicle(25.5, 224.0) def test_vehicle_initialized(self): self.assertEqual(25.5, self.vehicle.fuel) self.assertEqual(25.5, self.vehicle.capacity) self.assertEqual(224.0, self.vehicle.horse_power) self.assertEqual(1.25, self.vehicle.fuel_consumption) def test_drive(self): self.vehicle.drive(10) self.assertEqual(13, self.vehicle.fuel) with self.assertRaises(Exception) as ex: self.vehicle.drive(30) self.assertEqual(ex.exception.args[0], "Not enough fuel") def test_refuel(self): self.vehicle.fuel = 0 self.vehicle.refuel(10) self.assertEqual(10, self.vehicle.fuel) with self.assertRaises(Exception) as ex: self.vehicle.refuel(50) self.assertEqual(ex.exception.args[0], "Too much fuel") def test_str_vehicle(self): expect = "The vehicle has 224.0 horse power with 25.5 fuel left and 1.25 fuel consumption" self.assertEqual(expect, self.vehicle.__str__())
class TestVehicle(unittest.TestCase): def setUp(self) -> None: self.car = Vehicle(50, 100) # def test_initialized_when_given_correct_fuel_horse_power(self): # test_car = Vehicle(60, 70) # self.assertEqual(1.25, test_car.DEFAULT_FUEL_CONSUMPTION) # self.assertEqual(60, test_car.fuel) # self.assertEqual(60, test_car.capacity) # self.assertEqual(70, test_car.horse_power) def test_drive_when_fuel_is_enough__should_decrease_fuel(self): self.car.drive(10) expected = 37.5 actual = self.car.fuel self.assertEqual(actual, expected) def test_drive__when_fuel_not_enough__should_raise(self): expected = "Not enough fuel" with self.assertRaises(Exception) as context: self.car.drive(200) self.assertIsNotNone(context.exception) self.assertEqual(expected, context.exception.args[0]) def test_drive_when_given_invalid_parameters(self): pass def test_refuel__when_given_valid_fuel_amount__should_increase_fuel(self): self.car.drive(10) self.car.refuel(12.5) expected = 50 self.assertEqual(expected, self.car.fuel) def test_refuel__when_given_valid_fuel_reach_capacity__should_raise(self): self.car.drive(10) with self.assertRaises(Exception) as context: self.car.refuel(20) expected = "Too much fuel" self.assertEqual(expected, context.exception.args[0]) def test_str_should_return_right_message(self): self.car.drive(10) expected = f"The vehicle has 100 " \ f"horse power with 37.5 " \ f"fuel left and 1.25 fuel consumption" actual = self.car.__str__() self.assertEqual(expected, actual)
class VehicleTests(unittest.TestCase): fuel = 10 horse_power = 100 def setUp(self) -> None: self.vehicle = Vehicle(self.fuel, self.horse_power) #opional test 1 def test_vehicle_init__expect_fuel_consumption_to_be_equal_to_default_fuel_consumption( self): self.assertEqual(self.vehicle.DEFAULT_FUEL_CONSUMPTION, self.vehicle.fuel_consumption) #optional test 2 def test_vehicle_init__expect_capacity_to_be_equal_to_fuel(self): self.assertEqual(self.vehicle.fuel, self.vehicle.capacity) def test_vehicle_drive__when_fuel_capacity_is_enough(self): self.vehicle.drive(8) self.assertEqual(0, self.vehicle.fuel) def test_vehicle_drive__when_fuel_capacity_is_not_enough__expect_exception( self): vehicle = Vehicle(self.fuel, self.horse_power) with self.assertRaises(Exception) as context: self.vehicle.drive(10) expected = "Not enough fuel" self.assertEqual(expected, str(context.exception)) def test_vehicle_refuel__when_fuel_too_much__expect_exception(self): self.vehicle.fuel = 10 self.vehicle.capacity = 20 fuel = 20 with self.assertRaises(Exception) as context: self.vehicle.refuel(fuel) expected = "Too much fuel" self.assertEqual(expected, str(context.exception)) def test_vehicle_refuel__when_fuel_value_is_correct(self): self.vehicle.fuel = 10 self.vehicle.capacity = 20 fuel = 9 self.vehicle.refuel(fuel) expected = 19 actual = self.vehicle.fuel self.assertEqual(expected, actual) def test_vehicle_str_method(self): result = self.vehicle.__str__() expected = f"The vehicle has {self.horse_power} " \ f"horse power with {self.fuel} fuel left and {self.vehicle.fuel_consumption} fuel consumption" self.assertEqual(result, expected)
class TestVehicle(unittest.TestCase): def setUp(self): self.vehicle = Vehicle(fuel=2.5, horse_power=240.25) def test_vehicle_check_instance_attributes_are_set(self): self.assertEqual(2.5, self.vehicle.fuel) self.assertEqual(240.25, self.vehicle.horse_power) self.assertEqual(2.5, self.vehicle.capacity) def test_capacity_unchanged_if_fuel_is_changed(self): self.assertEqual(2.5, self.vehicle.capacity) self.vehicle.fuel = 20 self.assertEqual(2.5, self.vehicle.capacity) def test_vehicle__drive_if_has_fuel_go(self): self.vehicle.drive(kilometers=1) actual_fuel = self.vehicle.fuel expected_fuel = 1.25 self.assertEqual(expected_fuel, actual_fuel) def test_vehicle__drive__if_doesnt_have_enough_fuel_raise_exception(self): with self.assertRaises(Exception) as ex: self.vehicle.drive(kilometers=100) self.assertEqual(2.5, self.vehicle.fuel) self.assertEqual("Not enough fuel", str(ex.exception)) def test_vehicle__refuel_if_fuel_is_equal_or_less_than_capacity(self): self.vehicle.drive(1) self.vehicle.refuel(1.25) actual_fuel = self.vehicle.fuel expected_fuel = 2.5 self.assertEqual(expected_fuel, actual_fuel) def test_vehicle__refuel__if_fuel_is_bigger_than_capacity_raise_exception( self): with self.assertRaises(Exception) as ex: self.vehicle.refuel(1) self.assertEqual('Too much fuel', str(ex.exception)) self.assertEqual(2.5, self.vehicle.fuel) def test_vehicle__printing_result(self): result = self.vehicle.__str__() expected_result = f"The vehicle has 240.25 horse power with 2.5 fuel left and 1.25 fuel consumption" self.assertEqual(expected_result, result)
class VehicleTest(unittest.TestCase): def setUp(self): Vehicle.DEFAULT_FUEL_CONSUMPTION = 1.25 self.car = Vehicle(100.0, 120.0) # self.car.capacity = self.car.fuel # self.car.fuel_consumption = Vehicle.DEFAULT_FUEL_CONSUMPTION def test_init(self): self.assertEqual(self.car.fuel, 100.0) self.assertEqual(self.car.capacity, 100.0) self.assertEqual(self.car.horse_power, 120.0) # self.assertEqual(self.car.capacity, self.car.fuel) # self.assertEqual(self.car.fuel_consumption, Vehicle.DEFAULT_FUEL_CONSUMPTION) def test_drive_method_raises_exception(self): with self.assertRaises(Exception) as exp: self.car.drive(90) self.assertEqual(exp.exception.args[0], "Not enough fuel") def test_drive_method_is_not_throwing_exception(self): self.car.drive(80) self.assertEqual(self.car.fuel, 0) def test_refuel_method_raises_exception(self): with self.assertRaises(Exception) as exp: self.car.refuel(20) self.assertEqual(exp.exception.args[0], "Too much fuel") # def test_refuel_method_is_not_throwing_exception(self): # self.car.drive(40) # self.car.refuel(10) # self.assertEqual(self.car.fuel, 60) def test_refuel_method_is_not_throwing_exception(self): self.car.refuel(0) self.assertEqual(self.car.fuel, 100) def test_string_method_displays_the_correct_result(self): result = self.car.__str__() self.assertEqual( result, f"The vehicle has {self.car.horse_power} horse power with {self.car.fuel} fuel left and {self.car.fuel_consumption} fuel consumption" )
class VehicleTest(unittest.TestCase): fuel = 50.0 horse_power = 100.0 def setUp(self) -> None: self.v = Vehicle(self.fuel, self.horse_power) def test_vehicle_init(self): self.assertEqual(self.fuel, self.v.fuel) self.assertEqual(50.0, self.v.capacity) self.assertEqual(self.horse_power, self.v.horse_power) self.assertEqual(1.25, self.v.fuel_consumption) def test_drive_1_km__when_enough_fuel__should_decrement_fuel(self): self.v.drive(1) expected = self.fuel - 1.25 self.assertEqual(expected, self.v.fuel) def test_drive_100_km__when_not_enough_fuel__should_raise_exception(self): with self.assertRaises(Exception) as exc: self.v.drive(100) msg = "Not enough fuel" self.assertEqual(msg, str(exc.exception)) def test_refuel__with_valid_fuel_10(self): self.v.fuel = 10 self.v.refuel(10) self.assertEqual(20, self.v.fuel) def test_refuel_with_more_fuel_than_capacity(self): with self.assertRaises(Exception) as exc: self.v.refuel(100) msg = "Too much fuel" self.assertEqual(msg, str(exc.exception)) def test_vehicle_string_representation(self): expected = "The vehicle has 100.0 horse power with 50.0 fuel left and 1.25 fuel consumption" actual = self.v.__str__() self.assertEqual(expected, actual)
class VehicleTests(unittest.TestCase): def setUp(self): self.vehicle = Vehicle(50, 100) def test_vehicle_init__expect_initialization(self): self.assertEqual(50, self.vehicle.fuel) self.assertEqual(50, self.vehicle.capacity) self.assertEqual(100, self.vehicle.horse_power) self.assertEqual(1.25, self.vehicle.fuel_consumption) def test_vehicle_drive__if_not_enough_fuel__expect_exception(self): with self.assertRaises(Exception) as ex: self.vehicle.drive(100) self.assertEqual("Not enough fuel", str(ex.exception)) def test_vehicle_drive__if_enough_fuel__execute(self): self.vehicle.drive(20) self.assertEqual(25, self.vehicle.fuel) def test_vehicle_refuel__if_over_capacity__expect_exception(self): with self.assertRaises(Exception) as ex: self.vehicle.refuel(25) self.assertEqual("Too much fuel", str(ex.exception)) def test_vehicle_refuel__if_within_capacity__execute(self): self.vehicle.drive(20) self.vehicle.refuel(10) self.assertEqual(35, self.vehicle.fuel) def test_vehicle_str__to_return_str_message(self): expected_msg = f"The vehicle has {self.vehicle.horse_power} " \ f"horse power with {self.vehicle.fuel} fuel left and {self.vehicle.fuel_consumption} fuel consumption" actual_msg = self.vehicle.__str__() self.assertEqual(expected_msg, actual_msg) def test_vehicle_cls_attr__expect_correct_values(self): self.assertEqual(1.25, self.vehicle.DEFAULT_FUEL_CONSUMPTION)
class TestVehicle(unittest.TestCase): def setUp(self): self.car = Vehicle(63, 163) def test_init(self): self.assertEqual(self.car.fuel, 63) self.assertEqual(self.car.horse_power, 163) self.assertEqual(self.car.capacity, 63) self.assertEqual(self.car.fuel_consumption, Vehicle.DEFAULT_FUEL_CONSUMPTION) def test_drive_out_of_reach(self): with self.assertRaises(Exception) as ex: self.car.drive(100) self.assertEqual(self.car.fuel, 63) self.assertEqual("Not enough fuel", str(ex.exception)) def test_drive_in_range(self): self.car.drive(4) self.assertEqual(self.car.fuel, 58) def test_refuel_with_too_much_fuel(self): with self.assertRaises(Exception) as ex: self.car.refuel(100) self.assertEqual(self.car.fuel, 63) self.assertEqual("Too much fuel", str(ex.exception)) def test_car_refuel(self): self.car.drive(40) self.car.refuel(7) self.assertEqual(self.car.fuel, 20) def test_str(self): self.assertEqual( self.car.__str__(), f"The vehicle has 163 horse power with 63 " f"fuel left and 1.25 fuel consumption")