Skip to content Skip to sidebar Skip to footer

Mock Exception With Side Effect Raised In Class Method And Caught In Calling Method Gives 'did Not Raise'

Using side_effect, I am trying to raise an exception when a mock is called but I get a DID NOT RAISE EXCEPTION error that I do not understand. Based largely on this answer, I have

Solution 1:

Pytest cannot detect an exception because in get_response_from_external_api you are catching all of them. Depending on your requirements, you have these options:

  1. Don't catch the exceptions in get_response_from_external_api.
  2. Catch the exceptions, do whatever you want and then re-raise them.
  3. Instead of detecting an exception with pytest.raises, use capsys fixture to capture what is being printed and make assertions on that output.

Post a Comment for "Mock Exception With Side Effect Raised In Class Method And Caught In Calling Method Gives 'did Not Raise'"