mardi 31 mai 2016

Django unit testing - assertRaises working when it shouldn't

I was code reviewing an API and I found this in a unit test. The test passes but I can't understand why.

class SomeTestCase(TestCase):

    def setUp(self):
        self.client = Client()

    @mock.patch("patch.a.thing", mock_function)
    def test_fail(self):
        request_data = {
            "some": "bad data"
        }

        response = self.client.post(
            "/path/to/api",
            json.dumps(request_data),
            content_type="application/json",
            HTTP_HEADERNAME=HEADER_VALUE
        )

        self.assertRaises(Exception, response)

We're expecting a 500 status code from the API as we're passing in bad data. Even if calling the API were to cause an exception, would that not be raise during the client.post casing the unit test to error? response is not a callable and therefore it is wrong to pass it to assertRaises, however the behaviour I would expect to see is that assertRaises does not get an exception and therefore the unit test fails. Can anyone explain why it behaves this way?

Aucun commentaire:

Enregistrer un commentaire