Browsed by
Tag: testing

Spying on Python functions

Spying on Python functions

I was writing test code for a Python project, and needed to spy on the functions of one of the main object’s, called “server”, object instances called “rest_client”. By simply replacing the “rest_client” instance with a mock that passes the actual call to the wrapped object, I’m able to execute the production code of “rest_client” while later on making assertions.

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…

Read More Read More