class TestCarClass(unittest.TestCase): def setUp(self): self.car1 = Car(0.0, 0.0) self.car2 = Car(1.0, 1.0) def tearDown(self): del self.car1 del self.car2 def test_K001_test_car_creation(self): 'Test creation of a Car object' result1 = self.car1.getAutomobileCount() self.assertEqual(result1, 2) result2 = self.car1.left() self.assertEqual(result2, -4.0) result3 = self.car1.right() self.assertEqual(result3, 4.0) result4 = self.car1.top() self.assertEqual(result4, 2.0) result5 = self.car1.bottom() self.assertEqual(result5, -1.5) result6 = self.car1.getCOM() self.assertEqual(result6, (0.0, 0.0)) result7 = self.car1.getAutoNum() self.assertEqual(result7, 1) result8 = self.car1.getAutoName() self.assertEqual(result8, 'Car')
def TEST002(): #TEST002: All shapes have different Centre of Mass (COM) with no overlap -> No overlap print('-' * 40) #List to store automobiles autos = [] #Create Multi-component objects car1 = Car(0.0, 0.0) bus1 = Bus(10.0, 10.0) truck1 = Truck(-20.0, 17.3) #Append the automobiles to the input list autos.append(car1) autos.append(bus1) autos.append(truck1) #Send the list of automobiles as input to the new method of detecting collisons. result = assignment.get_intersections(autos) #Clean Up del car1 del bus1 del truck1 expected_result = [] if (result == expected_result): print("No Overlap of any Automobiles detected: TEST002 Result is OK!!") else: print("Expected collisions are not found!! TEST002 Result is NOK!!") print("Expected: ", expected_result) print("Attained: ", result) global overallResult overallResult = False
def TEST001(): #TEST001: All shapes have the same Centre of Mass (COM) -> All shapes have overlap print('-' * 40) #List to store automobiles autos = [] #Create Multi-component objects car1 = Car(0.0, 0.0) bus1 = Bus(0.0, 0.0) truck1 = Truck(0.0, 0.0) #Append the automobiles to the input list autos.append(car1) autos.append(bus1) autos.append(truck1) #Send the list of automobiles as input to the new method of detecting collisons. result = assignment.get_intersections(autos) #Clean Up del car1 del bus1 del truck1 expected_result = [(1, 2), (1, 3), (2, 3)] if (result == expected_result): print("A perfect overlap of COM's is detected: TEST001 Result is OK!!") else: print("Expected collisions are not found!! TEST001 Result is NOK!!") print("Expected: ", expected_result) print("Attained: ", result) global overallResult overallResult = False
def TEST003(): #TEST003: A line of automobiles are touching, bumber to bumber # -> Expect overlap with at least one other automobiles, each # Some will have an overlap with two automobiles print('-' * 40) #List to store automobiles autos = [] #Create Multi-component objects car1 = Car(0.0, 1.5) bus1 = Bus(14.0, 6.0) car2 = Car(33, 1.5) truck1 = Truck(47, 6.0) #Append the automobiles to the input list autos.append(car1) autos.append(bus1) autos.append(car2) autos.append(truck1) #Send the list of automobiles as input to the new method of detecting collisons. result = assignment.get_intersections(autos) #Clean Up del car1 del bus1 del car2 del truck1 expected_result = [(1, 2), (2, 3), (3, 4)] if (result == expected_result): print("Expected collisions are found!! TEST003 Result is OK!!") else: print("Expected collisions are not found!! TEST003 Result is NOK!!") print("Expected: ", expected_result) print("Attained: ", result) global overallResult overallResult = False
def test_N005_test_bumper_touch(self): 'The front body of Car1 is touching the back body of car2' self.car2 = Car(8.0, 0.0) self.result = assignment.autoOverlap(self.car1, self.car2) self.assertEqual(self.result, True)
def test_N004_test_touching(self): 'The back wheel of car2 is touching the front body of car1' self.car2 = Car(7.5, 2.5) self.result = assignment.autoOverlap(self.car1, self.car2) self.assertEqual(self.result, True)
def test_N003_test_slight_overlap(self): 'Two cars with an appreciable overlap' self.car1 = Car(1.0, 1.0) self.result = assignment.autoOverlap(self.car1, self.car2) self.assertEqual(self.result, True)
def test_N002_test_no_overlap(self): 'Two Cars with no overlap' self.car1 = Car(5.5, 5.5) self.result = assignment.autoOverlap(self.car1, self.car2) self.assertEqual(self.result, False)
def setUp(self): self.car1 = Car(0.0, 0.0) self.car2 = Car(0.0, 0.0)
def test_car_instance(self): honda = Car('Honda') self.assertIsInstance(honda, Car, msg='The object should be an instance of the `Car` class')
class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def read_odometer(self): print("This car has " + str(self.odometer_reading) + " miles on it") def update_odometer(self, mileage): self.odometer_reading = mileage my_car = Car('Renault', 'Captur', 2016) my_car.odometer_reading = 1000 # OR my_car.update_odometer(1000) my_car.read_odometer() # Inheritance class ElectricCar(Car): def __init__(self, make, model, year): super().__init__(make, model, year) my_tesla = ElectricCar('tesla', 'model S', 2016) my_tesla.update_odometer(100) print(my_tesla.read_odometer())