mercredi 23 décembre 2015

How to use python mock.patch to mock a function?

The idea is to basically test a send sms method by mocking it and checking if the method got called with the given parameters or not. I want to mock the send_sms method in x.y.z module

So I have this class:

from x.y.z import send_sms 
class BaseClass: 
    def __init__(self):
        pass
    def send_sms(self):
        send_sms(a,b,c,d)

And my test file looks like this:

from x.y.base_class import BaseClass
from mock import patch

class BaseTestClass:
    def setup(self):
        self.base_class_obj = BaseClass()
    def teardown(self):
        pass
    def test_send_sms(self):
        with patch('base_class.BaseClass.send_sms') as send_sms_fn:
            self.base_class_obj.send_sms('987654321', 'hi')
            send_sms_fn.assert_called_once_with('987654321', 'hi')

Whenever I run test for this the test gets stuck. I think I am not mocking the function properly. Please help!

Aucun commentaire:

Enregistrer un commentaire