不引发异常
我的代码如下所示:
def getAllVehicles(self):
try:
vehobj = Vehicles.objects.all()
except VehicleDoesNotExists, e:
logger.debug("Exception in getAllVehicles() is :::: %s ", e)
return vehobj
这是我的测试用例:
def test_getAllVehicles(self):
Vehicles.objects.all().delete()
instance = Vehicles()
self.assertRaises(VehicleDoesNotExists, instance.getAllVehicles)
永远不会引发异常,但我要求:
Vehicles.objects.all()
引发异常。
代码覆盖率要求我执行 except 块。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Vehicles.objects.all()
会给你一个空列表......这是正确的行为。如果您想引发异常,您可以测试空列表并自己引发 VehicleDoesNotExists(...)...但我不明白您为什么要这样做。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.