1

I have the following defined in an exceptions.py file:

class Error(Exception):
    """Base exception raised by api wrapper"""

    def __init__(self, message: str):
        self.message = message
        super().__init__(self.message)


# HTTP response exceptions
class ApiBadRequestError(Error):
    """Bad Request –- Incorrect parameters."""

    def __init__(self, message: str):
        self.message = message
        super().__init__(self.message)

I then have a function that correctly raises an ApiBadRequestError exception.

In pytest I'm doing the following:

 def test_handle_request_response_raises_correct_exception_for_response_code(
        self, status_code, exception_type, client, create_response
    ):
        response = create_response(status_code=status_code)

        with pytest.raises(ApiBadRequestError) as e:
            a = client._check_response_codes(response)

which is failing the test because inside pytest.raises it's doing isintance(e, ApiBadRequestError) which is returning False. If I however change the test to the following:

 def test_handle_request_response_raises_correct_exception_for_response_code(
        self, status_code, exception_type, client, create_response
    ):
        response = create_response(status_code=status_code)

        with pytest.raises(Exception) as e:
            a = client._check_response_codes(response)

it passes as the raised exception is seen as an instance of Exception even though it's an ApiBadRequestError

Any help would be really appreciated as I'm completely stumped here.

0 Answers0