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): self.vehicle = Vehicle(100, 10) def test_constructor(self): self.assertEqual(self.vehicle.fuel, 100) self.assertEqual(self.vehicle.fuel_consumption, 1.25) self.assertEqual(self.vehicle.DEFAULT_FUEL_CONSUMPTION, 1.25) self.assertEqual(self.vehicle.capacity, 100) self.assertEqual(self.vehicle.horse_power, 10) def test_drive_if_consumes_fuel_correctly(self): self.vehicle.drive(8) self.assertEqual(self.vehicle.fuel, 90) def test_drive_if_returns_exception_when_fuel_not_enough(self): with self.assertRaises(Exception) as exc: self.vehicle.drive(10000) self.assertEqual('Not enough fuel', str(exc.exception)) def test_refuel_if_refuels_correctly(self): self.vehicle.fuel = 0 start = self.vehicle.fuel self.vehicle.refuel(10) self.assertEqual(start + 10, self.vehicle.fuel) def test_refuel_if_fuel_is_above_capacity(self): with self.assertRaises(Exception) as exc: self.vehicle.refuel(100) self.assertEqual('Too much fuel', str(exc.exception)) def test_if__str__returns_correct_thing(self): expected = f"The vehicle has {self.vehicle.horse_power} " \ f"horse power with {self.vehicle.fuel} fuel left and {self.vehicle.fuel_consumption} fuel consumption" self.assertEqual(expected, str(self.vehicle))
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 VehicleTests(unittest.TestCase): fuel = 50 capacity = fuel horse_power = 20 fuel_consumption = 1.25 def setUp(self): self.car = Vehicle(self.fuel, self.horse_power) def test_vehicle_init__expect_parameters_to_be_set(self): self.assertEqual(self.fuel, self.car.fuel) self.assertEqual(self.capacity, self.car.capacity) self.assertEqual(self.horse_power, self.car.horse_power) self.assertEqual(self.fuel_consumption, self.car.fuel_consumption) def test_vehicle_drive__when_needed_fuel_exceeds_fuel_expect_exception( self): with self.assertRaises(Exception) as ex: self.car.drive(50) expected_message = "Not enough fuel" actual_message = str(ex.exception) self.assertEqual(expected_message, actual_message) def test_vehicle_drive__when_needed_fuel_does_not_exceed_fuel_expect_fuel_to_decrease_with_nedded_fuel( self): self.car.drive(30) expected_fuel = 12.5 actual_fuel = self.car.fuel self.assertEqual(expected_fuel, actual_fuel) def test_vehicle_refuel__when_added_fuel_and_fuel_exceed_capacity_expect_exception( self): with self.assertRaises(Exception) as ex: self.car.refuel(10) expected_message = "Too much fuel" actual_message = str(ex.exception) self.assertEqual(expected_message, actual_message) def test_vehicle_refuel__when_added_fuel_and_fuel_do_not_exceed_capacity_expect_fuel_to_increase_with_added_fuel( self): self.car.fuel = 20 self.car.refuel(10) expected_fuel = 30 actual_fuel = self.car.fuel self.assertEqual(expected_fuel, actual_fuel) def test_vehicle_str__expect_string_representation_in_correct_format(self): expected = f"The vehicle has {self.horse_power} " \ f"horse power with {self.fuel} fuel left and {self.fuel_consumption} fuel consumption" actual = str(self.car) self.assertEqual(expected, actual)
def test(self): vehicle = Vehicle(50, 150) self.assertEqual(Vehicle.DEFAULT_FUEL_CONSUMPTION, 1.25) self.assertEqual(vehicle.fuel, 50) self.assertEqual(vehicle.horse_power, 150) self.assertEqual(vehicle.fuel_consumption, 1.25) vehicle.drive(100) self.assertEqual(vehicle.fuel, 50) family_car = FamilyCar(150, 150) family_car.drive(50) self.assertEqual(family_car.fuel, 0.0) family_car.drive(50) self.assertEqual(family_car.fuel, 0.0) self.assertEqual(family_car.__class__.__bases__[0].__name__, "Car")
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 TestVehicle(unittest.TestCase): default_fc = 1.25 def setUp(self): self.vehicle = Vehicle(50, 163) def test_vehicle_init_method_initialize_correctly(self): fuel = 50 hp = 163 self.assertEqual(self.vehicle.fuel, fuel) self.assertEqual(self.vehicle.capacity, fuel) self.assertEqual(self.vehicle.horse_power, hp) self.assertEqual(self.vehicle.fuel_consumption, TestVehicle.default_fc) def test_vehicle_drive_method_when_fuel_enough(self): needed_fuel = self.vehicle.fuel - (self.vehicle.fuel_consumption * 10) self.vehicle.drive(10) self.assertEqual(self.vehicle.fuel, needed_fuel) def test_vehicle_method_drive_when_fuel_not_enough_should_raises(self): with self.assertRaises(Exception) as ex: self.vehicle.drive(50) self.assertIsNotNone(str(ex.exception)) def test_vehicle_refuel_method_when_fuel_amount_not_exceed_fuel_capacity( self): self.vehicle.fuel -= 20 self.vehicle.refuel(10) self.assertEqual(self.vehicle.fuel, 40) def test_vehicle_refuel_method_when_fuel_amount_exceed_fuel_capacity_should_raises( self): with self.assertRaises(Exception) as ex: self.vehicle.refuel(1) self.assertIsNotNone(str(ex.exception)) def test_vehicle_str_method(self): target = f"The vehicle has 163 " \ f"horse power with 50 fuel left and 1.25 fuel consumption" self.assertEqual(str(self.vehicle), target)
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 TestVehicle(TestCase): def setUp(self): self.vehicle = Vehicle(50, 200) def test_instance_attr_are_set(self): self.assertEqual(50, self.vehicle.fuel) self.assertEqual(200, self.vehicle.horse_power) self.assertEqual(50, self.vehicle.capacity) self.assertEqual(Vehicle.DEFAULT_FUEL_CONSUMPTION, self.vehicle.DEFAULT_FUEL_CONSUMPTION) def test_check_capacity_unchanged_if_fuel_changed(self): self.assertEqual(50, self.vehicle.capacity) self.vehicle.fuel = 20 self.assertEqual(50, self.vehicle.capacity) def test_drive(self): self.vehicle.drive(10) self.assertEqual(37.5, self.vehicle.fuel) def test_drive_more_than_amount_of_fuel_raises(self): with self.assertRaises(Exception) as ex: self.vehicle.drive(50) self.assertEqual(50, self.vehicle.fuel) self.assertEqual("Not enough fuel", str(ex.exception)) def test_refuel(self): self.vehicle.fuel = 20 self.vehicle.refuel(20) self.assertEqual(40, self.vehicle.fuel) def test_refuel_overflow_raises(self): with self.assertRaises(Exception) as ex: self.vehicle.refuel(10) self.assertEqual(50, self.vehicle.fuel) self.assertEqual("Too much fuel", str(ex.exception)) def test_str_representation(self): self.assertEqual(f"The vehicle has 200 horse power with 50 fuel left and 1.25 fuel consumption", str(self.vehicle))
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 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) -> None: self.v = Vehicle(10.5, 3.2) def test_vehicle_init(self): vehicle = Vehicle(10.5, 3.2) self.assertEqual((10.5, 3.2), (vehicle.fuel, vehicle.horse_power)) self.assertEqual((10.5, 1.25, 1.25), (vehicle.capacity, vehicle.fuel_consumption, vehicle.DEFAULT_FUEL_CONSUMPTION)) def test_vehicle_drive_successful_when_enough_fuel(self): self.v.drive(5) self.assertEqual(4.25, self.v.fuel) def test_vehicle_drive_raises_when_not_enough_fuel(self): with self.assertRaises(Exception) as ctx: self.v.drive(10) msg = "Not enough fuel" self.assertEqual(10.5, self.v.fuel) self.assertEqual(msg, str(ctx.exception)) def test_refuel_successful_when_valid_fuel(self): self.v.drive(2) self.v.refuel(1) self.assertEqual(9.0, self.v.fuel) def test_refuel_raises_when_too_much_fuel(self): self.v.drive(2) with self.assertRaises(Exception) as ctx: self.v.refuel(10) msg = "Too much fuel" self.assertEqual(msg, str(ctx.exception)) self.assertEqual(8.0, self.v.fuel) def test_vehicle_string_representation(self): msg = "The vehicle has 3.2 horse power with 10.5 fuel left and 1.25 fuel consumption" self.assertEqual(msg, str(self.v))
class TestVehicle(TestCase): fuel_consumption = 1.25 fuel = 50 capacity = 50 horse_power = 200 def setUp(self): self.vehicle = Vehicle(self.fuel, self.horse_power) def test_check_instance_attr_are_set(self): self.assertEqual(50, self.vehicle.fuel) self.assertEqual(50, self.vehicle.capacity) self.assertEqual(200, self.vehicle.horse_power) self.assertEqual(1.25, self.vehicle.fuel_consumption) def test_needed_fuel_to_drive(self): expected_needed_fuel = 6.25 actual_needed_fuel = self.vehicle.fuel_consumption * 5 self.assertEqual(expected_needed_fuel, actual_needed_fuel) def test_drive__when_enough_fuel__expected_to_reduced_fuel(self): self.vehicle.drive(5) self.assertEqual(43.75, self.vehicle.fuel) def test_drive__when_not_enough_fuel__expected_do_not_change_fuel(self): with self.assertRaises(Exception) as ex: self.vehicle.drive(49) self.assertEqual(50, self.vehicle.fuel) self.assertEqual('Not enough fuel', str(ex.exception)) def test_refuel_method(self): self.vehicle.drive(5) self.assertEqual(43.75, self.vehicle.fuel) self.vehicle.refuel(1) self.assertEqual(44.75, self.vehicle.fuel) def test_refuel__when_not_enough_capacity(self): with self.assertRaises(Exception) as ex: self.vehicle.refuel(1) self.assertEqual(50, self.vehicle.capacity) self.assertEqual('Too much fuel', str(ex.exception)) def test_str_representation(self): self.assertEqual( 'The vehicle has 200 horse power with 50 fuel left and 1.25 fuel consumption', str(self.vehicle))
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 TestVehicle(unittest.TestCase): def setUp(self): self.vehicle = Vehicle(50, 200) def test_check_instance_attr_are_set(self): self.assertEqual(50, self.vehicle.fuel) self.assertEqual(200, self.vehicle.horse_power) self.assertEqual(50, self.vehicle.capacity) self.assertEqual(self.vehicle.fuel_consumption, Vehicle.DEFAULT_FUEL_CONSUMPTION) def test_check_capacity_unchanged_if_fuel_changed(self): self.assertEqual(50, self.vehicle.capacity) self.vehicle.fuel = 20 self.assertEqual(50, self.vehicle.capacity) def test_drive_car(self): self.vehicle.drive(5) self.assertEqual(43.75, self.vehicle.fuel) def test_drive_not_successful(self): with self.assertRaises(Exception) as exc: self.vehicle.drive(49) self.assertEqual(50, self.vehicle.fuel) self.assertEqual('Not enough fuel', str(exc.exception)) def test_refuel_raises_when_more_than_capacity(self): with self.assertRaises(Exception) as exc: self.vehicle.refuel(1) self.assertEqual(50, self.vehicle.fuel) self.assertEqual('Too much fuel', str(exc.exception)) def test_refuel_success(self): self.vehicle.drive(5) self.assertEqual(43.75, self.vehicle.fuel) self.vehicle.refuel(1) self.assertEqual(44.75, self.vehicle.fuel) def test_representation_dunder(self): self.assertEqual( "The vehicle has 200 horse power with 50 fuel left and 1.25 fuel consumption", str(self.vehicle))
class TestVehicle(TestCase): def setUp(self): self.vehicle = Vehicle(50, 200) def test_check_instance_attributes_are_set(self): self.assertEqual(50, self.vehicle.fuel) self.assertEqual(200, self.vehicle.horse_power) self.assertEqual(50, self.vehicle.capacity) self.assertEqual(self.vehicle.fuel_consumption, Vehicle.DEFAULT_FUEL_CONSUMPTION) def test_chek_capacity_unchanged_if_fuel_changed(self): self.assertEqual(50, self.vehicle.capacity) self.vehicle.fuel = 20 self.assertEqual(50, self.vehicle.capacity) def test_drive_car(self): self.vehicle.drive(5) self.assertEqual(43.75, self.vehicle.fuel, msg="Success!Could be able to drive when there is enough fuel!") def test_drive_not_successful(self): with self.assertRaises(Exception) as ex: self.vehicle.drive(49) self.assertEqual(50, self.vehicle.fuel) self.assertEqual("Not enough fuel", str(ex.exception)) def test_refuel_raises_when_add_more_fuel_than_capacity(self): with self.assertRaises(Exception) as ex: self.vehicle.refuel(1) self.assertEqual(50, self.vehicle.fuel) self.assertEqual("Too much fuel", str(ex.exception)) def test_refuel_successful(self): self.vehicle.drive(5) self.assertEqual(43.75, self.vehicle.fuel) self.vehicle.refuel(1) self.assertEqual(44.75, self.vehicle.fuel) def test_string_representation(self): self.assertEqual("The vehicle has 200 horse power with 50 fuel left and" " 1.25 fuel consumption", str(self.vehicle))
class TestVehicle(unittest.TestCase): def setUp(self): self.vehicle = Vehicle(50, 100) def test_init_atr(self): self.assertEqual(50, self.vehicle.fuel) self.assertEqual(100, self.vehicle.horse_power) self.assertEqual(50, self.vehicle.capacity) self.assertEqual(self.vehicle.fuel_consumption, Vehicle.DEFAULT_FUEL_CONSUMPTION) def test_capacity_unchanged_when_if_fuel_changed(self): self.assertEqual(50, self.vehicle.capacity) self.vehicle.fuel = 20 self.assertEqual(50, self.vehicle.capacity) def test_drive_raises_exception(self): with self.assertRaises(Exception) as ex: self.vehicle.drive(50) self.assertEqual(50, self.vehicle.fuel) self.assertEqual("Not enough fuel", str(ex.exception)) def test_drive_success(self): self.vehicle.drive(5) self.assertEqual(43.75, self.vehicle.fuel) def test_refuel_success(self): self.vehicle.drive(5) self.vehicle.refuel(5) self.assertEqual(48.75, self.vehicle.fuel) def test_refuel_raises_exception_when_fuel_above_capacity(self): with self.assertRaises(Exception) as ex: self.vehicle.refuel(50) self.assertEqual(50, self.vehicle.fuel) self.assertEqual("Too much fuel", str(ex.exception)) def test_str_method(self): result = str(self.vehicle) self.assertEqual("The vehicle has 100 horse power with 50 fuel left and 1.25 fuel consumption", result)
class TestVehicle(TestCase): def setUp(self): self.vehicle = Vehicle(50, 200) def test_init_expect_correct_attr(self): self.assertEqual(50, self.vehicle.fuel) self.assertEqual(200, self.vehicle.horse_power) self.assertEqual(50, self.vehicle.capacity) self.assertEqual(self.vehicle.fuel_consumption, Vehicle.DEFAULT_FUEL_CONSUMPTION) def test_capacity__when_fuel_is_changed__expect_capacity_remains(self): self.assertEqual(50, self.vehicle.capacity) self.vehicle.fuel = 20 self.assertEqual(50, self.vehicle.capacity) def test_drive__when_enough_fuel__expect_to_decrease_fuel(self): self.vehicle.drive(5) self.assertEqual(43.75, self.vehicle.fuel) def test_drive__when_not_enough_fuel__expect_exception(self): with self.assertRaises(Exception) as ex: self.vehicle.drive(1) self.assertEqual(50, self.vehicle.fuel) self.assertEqual('Not enough fuel', str(ex.exception)) def test_refuel__when_fuel_less_then_capacity__expect_fuel_increase(self): self.vehicle.drive(5) self.assertEqual(43.75, self.vehicle.fuel) self.vehicle.refuel(1) self.assertEqual(44.75, self.vehicle.fuel) def test_refuel__when_fuel_more_then_capacity__expect_exception(self): with self.assertRaises(Exception) as ex: self.vehicle.refuel(1) self.assertEqual(50, self.vehicle.fuel) self.assertEqual('Too much fuel', str(ex.exception)) def test_repr__when_called__expect_correct_str(self): self.assertEqual("The vehicle has 200 horse power with 50 fuel left and 1.25 fuel consumption", str(self.vehicle))
class TestsVehicle(unittest.TestCase): def setUp(self): self.v = Vehicle(10.1, 100.1) def test_correct_initiolization_v(self): self.assertEqual(10.1, self.v.fuel) self.assertEqual(100.1, self.v.horse_power) self.assertEqual(10.1, self.v.capacity) self.assertEqual(1.25, self.v.fuel_consumption) def test_drive_when_fuel_not_enoght_raise_exeption_str(self): with self.assertRaises(Exception) as contex: self.v.drive(10) self.assertEqual("Not enough fuel", str(contex.exception)) def test_drive_when_fuel_enoght_decreases_fuel_with_consuptuon_mul_kilometers( self): self.v.drive(1) self.assertEqual(8.85, self.v.fuel) def test_refuel_when_fuel_is_more_than_capacity_raises_exeption(self): with self.assertRaises(Exception) as contex: self.v.refuel(10.2) self.assertEqual("Too much fuel", str(contex.exception)) def test_refuel_when_fuel_is_less_than_capacity_add_given_quantity_to_fuel( self): self.v.drive(1) self.v.refuel(1.25) self.assertEqual(10.1, self.v.fuel) def test_correct__str__rerpresantation(self): expected = (f"The vehicle has 100.1 " f"horse power with 10.1 fuel left and 1.25 " f"fuel consumption") self.assertEqual(expected, str(self.v))
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")
from project.vehicle import Vehicle from project.family_car import FamilyCar if __name__ == "__main__": vehicle = Vehicle(50, 150) print(Vehicle.DEFAULT_FUEL_CONSUMPTION) print(vehicle.fuel) print(vehicle.horse_power) print(vehicle.fuel_consumption) vehicle.drive(100) print(vehicle.fuel) family_car = FamilyCar(150, 150) family_car.drive(50) print(family_car.fuel) family_car.drive(50) print(family_car.fuel) print(family_car.__class__.__bases__[0].__name__)