OSMadeIT OSMadeIT - 1 year ago 139
Python Question

Car class Python

Create a
class that can be used to instantiate various vehicles.

It takes in arguments that depict the type, model, and name of the vehicle, provided they are set.

Let the test guide you to building your Car boiler-plate.

Here are the tests that guided me

class CarClassTest(TestCase):
"""docstring for CarClassTest"""

def test_car_instance(self):
honda = Car('Honda')
self.assertIsInstance(honda, Car, msg='The object should be an instance of the `Car` class')

def test_object_type(self):
honda = Car('Honda')
self.assertTrue((type(honda) is Car), msg='The object should be a type of `Car`')

def test_default_car_name(self):
gm = Car()
self.assertEqual('General', gm.name,
msg='The car should be called `General` if no name was passed as an argument')

def test_default_car_model(self):
gm = Car()
self.assertEqual('GM', gm.model, msg="The car's model should be called `GM` if no model was passed as an argument")

def test_car_properties(self):
toyota = Car('Toyota', 'Corolla')
self.assertListEqual(['Toyota', 'Corolla'],
[toyota.name, toyota.model],
msg='The car name and model should be a property of the car')

def test_car_doors(self):
opel = Car('Opel', 'Omega 3')
porshe = Car('Porshe', '911 Turbo')
Car('Koenigsegg', 'Agera R').num_of_doors],
[4, 2, 2],
msg='The car shoud have four (4) doors except its a Porshe or Koenigsegg')

def test_car_wheels(self):
man = Car('MAN', 'Truck', 'trailer')
koenigsegg = Car('Koenigsegg', 'Agera R')
self.assertEqual([8, 4], [man.num_of_wheels, koenigsegg.num_of_wheels],
msg='The car shoud have four (4) wheels except its a type of trailer')

def test_car_type(self):
koenigsegg = Car('Koenigsegg', 'Agera R')
msg='The car type should be saloon if it is not a trailer')

def test_car_speed(self):
man = Car('MAN', 'Truck', 'trailer')
parked_speed = man.speed
moving_speed = man.drive(7).speed

self.assertListEqual([parked_speed, moving_speed],
[0, 77],
msg='The Trailer should have speed 0 km/h until you put `the pedal to the metal`')

def test_car_speed2(self):
man = Car('Mercedes', 'SLR500')
parked_speed = man.speed
moving_speed = man.drive(3).speed

self.assertListEqual([parked_speed, moving_speed],
[0, 1000],
msg='The Mercedes should have speed 0 km/h until you put `the pedal to the metal`')

def test_drive_car(self):
man = Car('MAN', 'Truck', 'trailer')
moving_man = man.drive(7)
moving_man_instance = isinstance(moving_man, Car)
moving_man_type = type(moving_man) is Car
self.assertListEqual([True, True, man.speed],
[moving_man_instance, moving_man_type, moving_man.speed],
msg='The car drive function should return the instance of the Car class')

And here is the code that I have so far

class Car(object):
speed = 0

def __init__(self, name='General', model='GM', vehicle_type=None):
self.name = name
self.model = model
self.vehicle_type = vehicle_type

if self.name in ['Porshe', 'Koenigsegg']:
self.num_of_doors = 2
self.num_of_doors = 4

if self.vehicle_type == 'trailer':
self.num_of_wheels = 8
self.num_of_wheels = 4

def is_saloon(self):
if self.vehicle_type is not 'trailer':
self.vehicle_type == 'saloon'
return True
return False

def drive(self, moving_speed):
if moving_speed == 3:
Car.speed = 1000
elif moving_speed == 7:
Car.speed = 77

return self

Finally the error:

Total Specs: 11 Total Failures: 1

1 . test_car_speed2 Failure in line 68, in test_car_speed2 msg='The
Mercedes should have speed 0 km/h until you put
the pedal to the
') AssertionError: The Mercedes should have speed 0 km/h until
you put
the pedal to the metal

Answer Source

You need to set self.speed = 0 in __init__(), rather than setting speed = 0 at the class level. This is because you want a different speed for each Car that is created (currently you have just one speed in the whole world, shared by all cars).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download