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)
class TestTruck(unittest.TestCase): def setUp(self): self.fuel_quantity = 100 self.fuel_consumption = 3 self.truck = Truck(self.fuel_quantity, self.fuel_consumption) def test_carDrive_whenNotEnoughFuel(self): distance = 40 self.truck.drive(distance) self.assertEqual(self.fuel_quantity, self.truck.fuel_quantity) def test_carDrive_whenEnoughFuel(self): distance = 10 self.truck.drive(distance) expected = self.fuel_quantity - ( self.truck.fuel_consumption + self.truck.INCREASED_CONSUMPTION) * distance self.assertEqual(expected, self.truck.fuel_quantity) def test_carRefuel_shouldIncreaseFuelQuantity(self): amount = 10 self.truck.refuel(amount) self.assertEqual(self.fuel_quantity + (amount * 0.95), self.truck.fuel_quantity)
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)
# chapter_6.py from vehicle import Vehicle, Car, Truck my_car = Car("Ford", "Thunderbird") print(f"my_car is type {type(my_car)}") print(f"my_car uses {my_car.fuel}") print(f"my_car is a Car: {isinstance(my_car, Car)}") print(f"my_car is a Vehicle: {isinstance(my_car, Vehicle)}") print(f"Car is a subclass of Vehicle: {issubclass(Car, Vehicle)}") my_truck = Truck("Ford", "F350") print(f"my_truck is type {type(my_truck)}") print(f"my_truck uses {my_truck.fuel}") print(f"my_truck has {my_truck.number_of_wheels} wheels")
def setUp(self) -> None: self.car = Car(2, 1) self.truck = Truck(2, 1)
def setUp(self): self.c = Car(20, 5) self.t = Truck(20, 5)
def setUp(self): self.fuel_quantity = 100 self.fuel_consumption = 3 self.truck = Truck(self.fuel_quantity, self.fuel_consumption)
#!/usr/bin/env ############################################ # test_vehicle.py # Author: Paul Yang # Date: June, 2016 # Brief: this is to show HOWTO of python class, __init__method, accessing instance/class attribute ############################################ from vehicle import Truck, Sportscar, Compactcar cars = [ Truck('Mitush'), Truck('Orangetruck'), Sportscar('Z3'), Compactcar('Polo') ] print("--- test abstract methods ---") for car in cars: print(car.name + ': ' + car.move()) print("--- test common methods with different object ---") for car in cars: print(car.name + ': ' + drive(car)) print("--- test in inherited methods turbo ---") yaris = Compactcar('Yaris') yaris.turbo_move()
from flask import Flask, jsonify from vehicle import Truck app = Flask(__name__) host = '127.0.0.1' port = 5000 debug = True truk = Truck() asli = truk.state() jml = len(asli) @app.route('/') def home(): data = { 'status_code':200, 'message':'OK' } return jsonify(responses=data,data=asli,car=jml) @app.route('/position') def position(): data = { 'status_code':200, 'message':'OK' } for i in range(0,jml): jarak = asli[i]['distance'] arah = asli[i]['head'] vehicle_number = asli[i]['vehicle_number'] if arah == 'L': if jarak == 0 and arah == 'L':