Patching Python instance methods

Patching Python instance methods

I’m working on a project in which one of our services need to publish messages to a RabbitMQ exchange. Consider this code excerpt from the message queue client:

In my unit tests I wanted to verify that an unsuccessful attempt to publish the message, would cause trigger an exception to be thrown. To test this, I needed basic_publish  to return False. At first I found it tricky to patch instance methods such as basic_publish , but after coming across this excellent article over at Medium, I got the hang of it, and got my test code up and running:


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: