I would like to write some test in a way that are executed for all classes that inherit from a Parent.
For example I have the class motor with two specializations:
class Motor():
def run(self, energy):
pass
class ElectricMotor(Motor):
def run(self, electric_energy):
heat = electric_energy * 0.99
motion = electric_energy * 0.01
return heat, motion
class DieselMotor(Motor):
def run(self, diesel_energy):
heat = diesel_energy * 0.65
motion = diesel_energy * 0.35
return heat, motion
Then I have two tests which apply to every kind of motor:
class MotorTest(unittest.TestCase):
def test_energy_should_be_conserved():
for class_instance in all_motor_child_classes:
energy=10
assert sum(class_instance.run(energy))==energy
energy=20
assert sum(class_instance.run(energy))==energy
def test_motors_should_produce_heat():
for class_instance in all_motor_child_classes:
energy = 10
heat, motion=class_instance.run(energy)
assert heat>0
What I'm looking for is a way to do the loop
for class_instance in all_motor_child_classes:
or a different programming pattern to obtain the same result.
Any idea? Thanks Riccardo
Aucun commentaire:
Enregistrer un commentaire