mercredi 30 octobre 2019

Django Python Testing: How do I assert that some exception happened and was handled

I'm trying to test a Django view using the client post method.

Inside the view I catch an exception and return a 500 response

My test that checks the 500 response passes however I would also now like to test what actual Exception class was raised during the view call.

Is there a way to do that?

I've tried self.assertRaises(SpecificException) as a context manager but since it is already handled and caught, it won't do what I'm looking for:

with self.assertRaises(SpecificException):
    response = self.client.post(
        path=reverse("stock:getstockinvt"),
        data={'product_id': 1},
        content_type='application/json'
    )

If this can't be done then so be it, I am just wondering if this can.

The alternative is to unit test the function that raises the exception.

Aucun commentaire:

Enregistrer un commentaire