def test_carDrive_whenEnoughFuel_shouldDecreaseFuel(self): make = 'test make' model = 'test model' fuel_consumption = 6 fuel_capacity = 60 params = [make, model, fuel_consumption, fuel_capacity] car = Car(*params) with self.assertRaises(Exception) as context: car.drive(100) self.assertIsNotNone(context.exception)
class CarManagerTests(unittest.TestCase): def setUp(self): self.car = Car("a", 'b', 1, 4) def test_init(self): self.assertEqual(self.car.make, "a") self.assertEqual(self.car.model, "b") self.assertEqual(self.car.fuel_consumption, 1) self.assertEqual(self.car.fuel_capacity, 4) def test_make_raises_exception_when_empty(self): with self.assertRaises(Exception): self.car.make = "" def test_model_raises_exception_when_empty(self): with self.assertRaises(Exception): self.car.model = "" def test_fuel_consumption_when_negative_raises_exception(self): with self.assertRaises(Exception): self.car.fuel_consumption = -50 def test_fuel_consumption_when_zero_raises_exception(self): with self.assertRaises(Exception): self.car.fuel_consumption = 0 def test_fuel_capacity_when_negative_raises_exception(self): with self.assertRaises(Exception): self.car.fuel_capacity = -50 def test_fuel_capacity_when_zero_raises_exception(self): with self.assertRaises(Exception): self.car.fuel_capacity = 0 def test_refuel_more_than_capacity(self): self.car.refuel(50) self.assertEqual(self.car.fuel_amount, self.car.fuel_capacity) def test_refuel_raises_exception_when_negative(self): with self.assertRaises(Exception): self.car.refuel(-50) def test_drive_more_than_fuel(self): with self.assertRaises(Exception): self.car.drive(1000) def test_drive(self): self.car.refuel(4) self.car.drive(400) self.assertEqual(self.car.fuel_amount, 0)
def test_carDrive_whenNotEnoughFuel_shouldRaise(self): make = 'test make' model = 'test model' fuel_consumption = 6 fuel_capacity = 60 params = [make, model, fuel_consumption, fuel_capacity] car = Car(*params) car.refuel(car.fuel_capacity) distance = 100 car.drive(distance) expected = car.fuel_capacity - car.fuel_consumption * distance / 100 actual = car.fuel_amount self.assertEqual(expected, actual)
class CarTest(unittest.TestCase): def setUp(self): self.car = Car("a", "b", 1, 4) def test_init(self): self.assertEqual(self.car.make, 'a') self.assertEqual(self.car.model, 'b') self.assertEqual(self.car.fuel_consumption, 1) self.assertEqual(self.car.fuel_capacity, 4) def test_make_empty_raises_exception(self): with self.assertRaises(Exception): self.car.make = '' def test_model_make_empty_raises_exception(self): with self.assertRaises(Exception): self.car.make = '' def test_fuel_consumption_negative_raises_exception(self): with self.assertRaises(Exception): self.car.fuel_consumption = -42 def test_fuel_capacity_negative_raises_exception(self): with self.assertRaises(Exception): self.car.fuel_capacity = -42 def test_refuel_negative_raises_exception(self): with self.assertRaises(Exception): self.car.refuel(-42) def test_refuel_more_then_capacity(self): self.car.refuel(42) self.assertEqual(self.car.fuel_amount, self.car.fuel_capacity) def test_drive_more_then_fuel(self): with self.assertRaises(Exception) as exc: self.car.drive(1000) self.assertEqual(str(exc.exception), 'You don\'t have enough fuel to drive!') def test_fuel_amount_after_drive(self): self.car.refuel(4) self.car.drive(400) self.assertEqual(self.car.fuel_amount, 0)
class CarTests(unittest.TestCase): def setUp(self) -> None: self.car = Car("name", "model", 1, 3) def test_init_method(self): self.assertEqual(self.car.make, "name") self.assertEqual(self.car.model, "model") self.assertEqual(self.car.fuel_consumption, 1) self.assertEqual(self.car.fuel_capacity, 3) self.assertEqual(self.car.fuel_amount, 0) def test_make_setter_empty_str_exception(self): with self.assertRaises(Exception) as context: self.car.make = "" self.assertEqual(str(context.exception), "Make cannot be null or empty!") def test_make_setter_correct_str(self): self.car.make = "a" self.assertEqual(self.car.make, "a") def test_model_setter_empty_str_exception(self): with self.assertRaises(Exception) as context: self.car.model = "" self.assertEqual(str(context.exception), "Model cannot be null or empty!") def test_model_setter_correct_str(self): self.car.model = "a" self.assertEqual(self.car.model, "a") def test_fuel_consumption_zero_value(self): with self.assertRaises(Exception) as context: self.car.fuel_consumption = 0 self.assertEqual(str(context.exception), "Fuel consumption cannot be zero or negative!") def test_fuel_consumption_negative_value(self): with self.assertRaises(Exception) as context: self.car.fuel_consumption = -1 self.assertEqual(str(context.exception), "Fuel consumption cannot be zero or negative!") def test_correct_fuel_consumption_value(self): self.car.fuel_consumption = 2 self.assertEqual(self.car.fuel_consumption, 2) def test_fuel_capacity_zero_value(self): with self.assertRaises(Exception) as context: self.car.fuel_capacity = 0 self.assertEqual(str(context.exception), "Fuel capacity cannot be zero or negative!") def test_fuel_capacity_negative_value(self): with self.assertRaises(Exception) as context: self.car.fuel_capacity = -1 self.assertEqual(str(context.exception), "Fuel capacity cannot be zero or negative!") def test_correct_fuel_capacity_value(self): self.car.fuel_capacity = 2 self.assertEqual(self.car.fuel_capacity, 2) def test_fuel_amount_negative_value(self): with self.assertRaises(Exception) as context: self.car.fuel_amount = -1 self.assertEqual(str(context.exception), "Fuel amount cannot be negative!") def test_fuel_amount_correct_value(self): self.car.fuel_amount = 1 self.assertEqual(self.car.fuel_amount, 1) def test_refuel_negative_zero_amount_fuel(self): with self.assertRaises(Exception) as context: self.car.refuel(0) self.assertEqual(str(context.exception), "Fuel amount cannot be zero or negative!") def test_max_refuel_amount_equal_capacity(self): self.car.refuel(100) self.assertEqual(self.car.fuel_amount, self.car.fuel_capacity) def test_refuel_amount_less_than_capacity(self): self.car.refuel(1) self.assertEqual(self.car.fuel_amount, 1) def test_drive_more_distance_than_fuel_raise_exception(self): with self.assertRaises(Exception) as context: self.car.drive(1) self.assertEqual(str(context.exception), "You don't have enough fuel to drive!") def test_drive_distance_reduce_fuel_amount(self): self.car.refuel(1) self.car.drive(100) self.assertEqual(self.car.fuel_amount, 0)
class TestCar(unittest.TestCase): make = "TestMake" model = "TestModel" fuel_consumption = 5 fuel_capacity = 20 def setUp(self): self.car = Car(self.make, self.model, self.fuel_consumption, self.fuel_capacity) def test_car_make__when_value_is_not_none__expect_to_change_make(self): self.car.make = "test" self.assertEqual("test", self.car.make) def test_car_make__when_value_is_none__expect_exception(self): with self.assertRaises(Exception) as context: self.car.make = None self.assertEqual("Make cannot be null or empty!", str(context.exception)) def test_car_model__when_value_is_not_none__expect_to_change_model(self): self.car.model = "test" self.assertEqual("test", self.car.model) def test_car_model__when_value_is_none__expect_exception(self): with self.assertRaises(Exception) as context: self.car.model = None self.assertEqual("Model cannot be null or empty!", str(context.exception)) def test_car_fuel_consumption__when_value_is_above_0__expect_to_change_fuel_consumption( self): self.car.fuel_consumption = 2 self.assertEqual(2, self.car.fuel_consumption) def test_car_fuel_consumption__when_value_is_0__expect_exception(self): with self.assertRaises(Exception) as context: self.car.fuel_consumption = 0 self.assertEqual("Fuel consumption cannot be zero or negative!", str(context.exception)) def test_car_fuel_consumption__when_value_is_below_0__expect_exception( self): with self.assertRaises(Exception) as context: self.car.fuel_consumption = -1 self.assertEqual("Fuel consumption cannot be zero or negative!", str(context.exception)) def test_car_fuel_capacity__when_value_is_above_0__expect_to_change_fuel_capacity( self): self.car.fuel_capacity = 2 self.assertEqual(2, self.car.fuel_capacity) def test_car_fuel_capacity__when_value_is_0__expect_exception(self): with self.assertRaises(Exception) as context: self.car.fuel_capacity = 0 self.assertEqual("Fuel capacity cannot be zero or negative!", str(context.exception)) def test_car_fuel_capacity__when_value_is_below_0__expect_exception(self): with self.assertRaises(Exception) as context: self.car.fuel_capacity = -1 self.assertEqual("Fuel capacity cannot be zero or negative!", str(context.exception)) def test_car_fuel_amount__when_value_is_above_0__expect_to_change_fuel_amount( self): self.car.fuel_amount = 2 self.assertEqual(2, self.car.fuel_amount) def test_car_fuel_amount__when_value_is_below_0__expect_exception(self): with self.assertRaises(Exception) as context: self.car.fuel_amount = -1 self.assertEqual("Fuel amount cannot be negative!", str(context.exception)) def test_car_refuel__when_fuel_is_above_0_and_fuel_amount_is_not_above_fuel_capacity__expect_to_add_value_to_fuel_amount( self): self.car.refuel(5) self.assertEqual(5, self.car.fuel_amount) def test_car_refuel__when_fuel_is_above_0_and_fuel_amount_is_above_fuel_capacity__expect_fuel_amount_and_fuel_capacity_to_be_equal( self): self.car.refuel(21) self.assertEqual(self.car.fuel_amount, self.car.fuel_capacity) def test_car_refuel__when_value_is_0__expect_exception(self): with self.assertRaises(Exception) as context: self.car.refuel(0) self.assertEqual("Fuel amount cannot be zero or negative!", str(context.exception)) def test_car_refuel__when_value_is_below_0__expect_exception(self): with self.assertRaises(Exception) as context: self.car.refuel(-1) self.assertEqual("Fuel amount cannot be zero or negative!", str(context.exception)) def test_car_drive__when_fuel_amount_is_enough__expect_fuel_amount_to_be_decreased( self): self.car.fuel_amount = 5 self.car.drive(100) self.assertEqual(0, self.car.fuel_amount) def test_car_drive__when_fuel_amount_is_not_enough__expect_exception(self): self.car.fuel_amount = 4 with self.assertRaises(Exception) as context: self.car.drive(100) self.assertEqual("You don't have enough fuel to drive!", str(context.exception))
class TestCar(unittest.TestCase): def setUp(self) -> None: self.car = Car('Audi', 'Q8', 6.7, 85) def test_init_(self): expected = { '_Car__make': 'Audi', '_Car__model': 'Q8', '_Car__fuel_consumption': 6.7, '_Car__fuel_capacity': 85, '_Car__fuel_amount': 0 } actual = self.car.__dict__ self.assertEqual(actual, expected) def test_make__set_to_given_value(self): self.car.make = 'BMW' expected = 'BMW' actual = self.car.make self.assertEqual(actual, expected) def test_make__raise_Exception_if_given_value_empty(self): with self.assertRaises(Exception) as cm: self.car.make = '' self.assertEqual(str(cm.exception), "Make cannot be null or empty!") def test_model__set_to_given_value(self): self.car.model = 'BMW' expected = 'BMW' actual = self.car.model self.assertEqual(actual, expected) def test_model__raise_Exception_if_given_value_empty(self): with self.assertRaises(Exception) as cm: self.car.model = '' self.assertEqual(str(cm.exception), "Model cannot be null or empty!") def test_fuel_consumption__set_to_given_value(self): self.car.fuel_consumption = 6.4 expected = 6.4 actual = self.car.fuel_consumption self.assertEqual(actual, expected) def test_fuel_consumption__raise_Exception_if_given_value_less_or_equal_zero( self): with self.assertRaises(Exception) as cm: self.car.fuel_consumption = 0 self.assertEqual(str(cm.exception), "Fuel consumption cannot be zero or negative!") def test_fuel_capacity__set_to_given_value(self): self.car.fuel_capacity = 60.4 expected = 60.4 actual = self.car.fuel_capacity self.assertEqual(actual, expected) def test_fuel_capacity__raise_Exception_if_given_value_less_or_equal_zero( self): with self.assertRaises(Exception) as cm: self.car.fuel_capacity = 0 self.assertEqual(str(cm.exception), "Fuel capacity cannot be zero or negative!") def test_fuel_amount__set_to_given_value(self): self.car.fuel_amount = 60.4 expected = 60.4 actual = self.car.fuel_amount self.assertEqual(actual, expected) def test_fuel_amount__raise_Exception_if_given_value_less_than_zero(self): with self.assertRaises(Exception) as cm: self.car.fuel_amount = -1 self.assertEqual(str(cm.exception), "Fuel amount cannot be negative!") def test_refuel__raise_Exception_if_given_value_less_or_equal_zero(self): with self.assertRaises(Exception) as cm: self.car.refuel(0) self.assertEqual(str(cm.exception), "Fuel amount cannot be zero or negative!") def test_refuel__increase_fuel_amount_by_given_value(self): self.car.fuel_amount = 60.4 self.car.refuel(20) expected = 80.4 actual = self.car.fuel_amount self.assertEqual(actual, expected) def test_refuel__set_to_fuel_capacity_if_new_value_exceeds_it(self): self.car.fuel_amount = 60.4 self.car.refuel(30) expected = 85 actual = self.car.fuel_amount self.assertEqual(actual, expected) def test_drive__raise_Exception_if_needed_fuel_exceeds_fuel_amount(self): with self.assertRaises(Exception) as cm: self.car.drive(10000) self.assertEqual(str(cm.exception), "You don't have enough fuel to drive!") def test_drive__decrease_fuel_amount_by_given_value(self): self.car.fuel_amount = 85 self.car.drive(100) expected = 78.3 actual = self.car.fuel_amount self.assertEqual(actual, expected)