I’m testing an API that can raise an exception, but eventually returns a response based on other calculations, not related to the exception that is raised (it only logs the exception):
def get(self, request):
#some code here
try:
#do something and create the response value
except Exception as err:
logger.exception(str(err))
return Response
I tried the following to test if an exception is raised, but it didn’t work as the final return value (API’s response) doesn’t care about the exception:
with pytest.raises(RuntimeError):
response = MyInfo.as_view(
)(request)
I get the following:
> with pytest.raises(RuntimeError):
E Failed: DID NOT RAISE <class 'RuntimeError'>
even though a RuntimeError exception is logged. I think pytest only cares about the API’s response and not an exception that has occurred inside the API.