pythonpatchmagicmock

python how to mock 3rd party library response


I'm using a 3rd party library "mailjet" to send email. Here's the doc: https://dev.mailjet.com/email/guides/getting-started/#prerequisites

This is the method in send_email.py that I want to test:

from mailjet_rest import Client

def send_email(email_content):
  client = Client(auth=(api_key, api_secret))
  response = client.send.create(data=email_content)
  if response.status_code != 200:
    // raise the error
  else:
    print(response.status_code)
    return response.status_code
...

I wanted to mock the status code. This is what I tried in test_send_email.py:

from unittest.mock import MagicMock, patch
import unittest
import send_email

class TestClass(unittest.TestCase):
    @patch("send_email.Client")
    def test_send_email(self, _mock):
        email_content = {...}
        _mock.return_value.status_code = 200
        response = send_email(email_content)
        // assert response == 200

When I run the test, it didn't print out the status code, it prints:

<MagicMock name='Client().send.create().status_code' id='140640470579328'>

How can I mock the status_code in the test? Thanks in advance!!!!!


Solution

  • You are putting the attribute in the wrong place.

    You mock the Client, which is correct, and put something in its return_value, which is also good.

    However, the line

    response = client.send.create(data=email_content)
    

    applies on the return_value of your mock (client), the send attribute and its create method.

    So what you need is to set the return_value.status_code of this create method.

    class TestClass(unittest.TestCase):
        @patch("send_email.Client")
        def test_send_email(self, mock_client):
            email_content = {...}
            # this puts 200 in Client().status_code
            # mock_client.return_value.status_code = 200
            # that puts 200 in Client().send.create().status_code
            mock_client.return_value.send.create.return_value.status_code = 20
            response = send_email(email_content)
            assert response == 200