My code is like below:
def getAllVehicles(self):
try:
vehobj = Vehicles.objects.all()
except VehicleDoesNotExists, e:
logger.debug("Exception in getAllVehicles() is :::: %s ", e)
return vehobj
And here is my test case:
def test_getAllVehicles(self):
Vehicles.objects.all().delete()
instance = Vehicles()
self.assertRaises(VehicleDoesNotExists, instance.getAllVehicles)
Exception is never raised, but I require that:
Vehicles.objects.all()
to raise an exception.
Code coverage is asking me to execute the except block.
Vehicles.objects.all()will give you an empty list… which is the correct behavior. If you want to raise an exception, you can test for an empty list andraise VehicleDoesNotExists(...)yourself… but I don’t understand why you’d want to.