Пример #1
0
class TestCar(unittest.TestCase):
    def setUp(self):
        self.fuel_quantity = 100
        self.fuel_consumption = 3
        self.car = Car(self.fuel_quantity, self.fuel_consumption)

    def test_carDrive_whenNotEnoughFuel(self):
        distance = 40
        self.car.drive(distance)

        self.assertEqual(self.fuel_quantity, self.car.fuel_quantity)

    def test_carDrive_whenEnoughFuel(self):
        distance = 10
        self.car.drive(distance)

        expected = self.fuel_quantity - (self.car.fuel_consumption + self.car.
                                         INCREASED_CONSUMPTION) * distance
        self.assertEqual(expected, self.car.fuel_quantity)

    def test_carRefuel_shouldIncreaseFuelQuantity(self):
        amount = 10
        self.car.refuel(amount)

        self.assertEqual(self.fuel_quantity + amount, self.car.fuel_quantity)
Пример #2
0
class TestVehicle(unittest.TestCase):
    def setUp(self):
        self.c = Car(20, 5)
        self.t = Truck(20, 5)

    def vehicle_is_abstract_class(self):
        with self.assertRaises(TypeError) as exc:
            Vehicle(20, 5)

    def test_drive_car_enough_fuel(self):
        expected = self.c.fuel_quantity - 3 * (5 + 0.9)
        self.c.drive(3)
        self.assertEqual(self.c.fuel_quantity, expected)

    def test_drive_car_not_enough_fuel(self):
        expected = self.c.fuel_quantity
        self.c.drive(100)
        self.assertEqual(self.c.fuel_quantity, expected)

    def test_drive_car_0km_no_change(self):
        expected = 0
        old_fuel_q = self.c.fuel_quantity
        self.c.drive(0)
        self.assertEqual(self.c.fuel_quantity - old_fuel_q, expected)

    def test_drive_truck_enough_fuel(self):
        expected = self.t.fuel_quantity - 3 * (5 + 1.6)
        self.t.drive(3)
        self.assertEqual(self.t.fuel_quantity, expected)

    def test_drive_truck_not_enough_fuel(self):
        expected = self.t.fuel_quantity
        self.t.drive(100)
        self.assertEqual(self.t.fuel_quantity, expected)

    def test_different_consumption_rate(self):
        self.c.drive(3)
        self.t.drive(3)
        self.assertNotEqual(self.c.fuel_quantity, self.t.fuel_quantity)

    def test_refuel_car(self):
        added_fuel = 20
        expected = self.c.fuel_quantity + added_fuel
        self.c.refuel(added_fuel)
        self.assertEqual(self.c.fuel_quantity, expected)

    def test_refuel_truck(self):
        added_fuel = 20
        expected = self.t.fuel_quantity + added_fuel * 0.95
        self.t.refuel(added_fuel)
        self.assertEqual(self.t.fuel_quantity, expected)
Пример #3
0
class VehicleTests(unittest.TestCase):
    def setUp(self) -> None:
        self.car = Car(2, 1)
        self.truck = Truck(2, 1)

    def test_car_init_method(self):
        self.assertEqual(self.car.fuel_quantity, 2)
        self.assertEqual(self.car.fuel_consumption, 1)

    def test_truck_init_method(self):
        self.assertEqual(self.truck.fuel_quantity, 2)
        self.assertEqual(self.truck.fuel_consumption, 1)

    def test_refuel_car(self):
        self.car.refuel(1)
        self.assertEqual(self.car.fuel_quantity, 3)

    def test_refuel_truck(self):
        self.truck.refuel(8)
        self.assertEqual(self.truck.fuel_quantity, 9.6)

    def test_car_drive_enough_fuel(self):
        self.car.fuel_quantity = 1.9
        self.car.drive(1)
        self.assertEqual(self.car.fuel_quantity, 0)

    def test_car_drive_not_enough_fuel(self):
        self.car.drive(10)
        self.assertEqual(self.car.fuel_quantity, 2)

    def test_truck_drive_enough_fuel(self):
        self.truck.fuel_quantity = 2.6
        self.truck.drive(1)
        self.assertEqual(self.truck.fuel_quantity, 0)

    def test_truck_drive_not_enough_fuel(self):
        self.truck.drive(10)
        self.assertEqual(self.truck.fuel_quantity, 2)