Search code examples
pythondjangounit-testingmockingpython-mock

I can't get mock to work


I'm trying to use a mock for unit testing with Python/Django but I just can't get it to work. The mock acts like it has never been called.

tests.py

from my_module import my_library
my_library = MagicMock()

class MyTest(TestCase):
    def test_add(self):
        acronym = 'TEST'

        m = MyModel(acronym=acronym)
        m.save()

        my_library.add.assert_called_with(acronym=acronym)

my_library.py

def add(acronym):
    # Processing...

models.py

class MyModel(Model):
    acronym = CharField(max_length=4)

    def save(self):
        my_library.add(acronym=self.acronym)

        super(MyModel, self).save(*args, **kwargs)

My library works, I know the function add is correctly called. But the mock doesn't seem to work because it just raises en exception when I run the unit tests:

AssertionError: Expected call: add(acronym='TEST')
Not called

I think I don't correctly use the mock thing. Can anyone help or give advice please?


Solution

  • That is because it is never called. The first line of your program does this:

    my_library = __import__("my_module")
    

    The next line just overshadows the first. It does not alter my_module in any way, thus all your other code just calls the original code instead of the mock.

    Take a look at mock.patch instead. Either like this:

    from mock import patch
    import my_module as my_library
    
    class MyTest(TestCase):
        def test_add(self):
            acronym = 'TEST'
            with patch.object(my_library, 'add') as add_method:   
                m = MyModel(acronym=acronym)
                m.save()
                add_method.assert_called_with(acronym=acronym)
    

    Or using the original module name:

    from mock import patch
    
    class MyTest(TestCase):
        def test_add(self):
            acronym = 'TEST'
            with patch('my_module.add') as add_method:   
                m = MyModel(acronym=acronym)
                m.save()
                add_method.assert_called_with(acronym=acronym)