4

私が書いたメール送信方法をテストしたいと思います。ファイル format_email.py に send_email をインポートします。

 from cars.lib.email import send_email

 class CarEmails(object):

    def __init__(self, email_client, config):
        self.email_client = email_client
        self.config = config

    def send_cars_email(self, recipients, input_payload):

send_cars_email() で電子メールの内容をフォーマットした後、以前にインポートした方法を使用して電子メールを送信します。

 response_code = send_email(data, self.email_client)

テストファイル test_car_emails.py で

@pytest.mark.parametrize("test_input,expected_output", test_data)
def test_email_payload_formatting(test_input, expected_output):
    emails = CarsEmails(email_client=MagicMock(), config=config())
    emails.send_email = MagicMock()
    emails.send_cars_email(*test_input)
    emails.send_email.assert_called_with(*expected_output)

テストを実行すると、アサーションが呼び出されずに失敗します。問題は send_email 関数を嘲笑しているところだと思います。

この関数をどこでモックする必要がありますか?

4

3 に答える 3