Skip to content Skip to sidebar Skip to footer

Exception Raised But Not Caught By Assertraises

I'm trying to test that my authentication fails. The exception is raised but not caught by assertRaises. What am I missing here? def test_auth(self): from graphql_jwt.exception

Solution 1:

Your test is not catching a PermissionDenied exception because something in the code you're running is wrapping that exception in an instance of graphql.error.located_error.GraphQLLocatedError. Because you're checking for the wrong exception type, the test fails.

I don't know too much about the libraries you're using, and the invisible change of the exception type seems like a horrible mis-feature (it should at least add the code that changes the exception type to the exception traceback so you can debug it). But you may be able to work around the issue, by catching the wrapped exception and rethrowing the original:

deftest_auth(self):
    from graphql_jwt.exceptions import PermissionDenied
    from graphql.error.located_error import GraphQLLocatedError

    with self.assertRaises(PermissionDenied):
        try:
            response = self.client.execute(self.query)
        except GraphQLLocatedError as e:
            raise e.original_error

Solution 2:

GraphQL, by definition catches all exceptions and puts errors in the errors part of the response. If you're testing the execution of a query (self.client.execute(... query ...)) you should get the result and and verify it has an errors part that matches what you expect.

An easier way would be to test the resolver specifically - call resolve_entity directly and not via the GraphQL execution layer, and test it like you would any other Python function.

Post a Comment for "Exception Raised But Not Caught By Assertraises"