Unitesting in python - Instance Methods

How interfaces can make it easier to mock out individual components of the codebase.

Table of contents

No heading

No headings in the article.

Suppose we have the following python code snippet :

class FileManager:
    def isStored(self):
        pass
    def read(self):
        pass

class FileManagerImpl(FileManager):
    def isStored(self):
        return True
    def read(self):
        return  "Read some data"

class SomeOtherViewModel:

    def readCache(self,fileManager):
        if (fileManager.isStored()):
            return  fileManager.read()
        return  None

If you are not familiar with python, the filemanager class in this case acts like an interface, since python does not have inbuilt support for interfaces. The filemanagerimpl implements the filemanager interface and the viewmodel class, or could be any other class, simply wants to use some functionality from the file manager.

How do we write unit tests for SomeOtherViewModel?

if you are not familiar with what unit testing is, it simply means that you want to write test cases to validate the functionality of individual sections of your code, might write a more detailed blog on this some other day.

Let's look at what the class needs. It needs an instance of fileManager, but since this is a unit test, and we are using an interface, we can simply mock out the interface and pass in an instance. Have a look at the following code snippet:

class MockFileManagerStored(FileManager):
    def isStored(self):
        return True

    def read(self):
        return "Returning mocked data"

The above is similar to FileManagerImpl , we can return whatever we want inside the instance methods.

We can use the above mock impl as follows:

class FileTestCase(TestCase):

    def test_viewModel(self):
        mockFile = MockFileManagerStored()
        someOtherClass = SomeOtherViewModel()
        result = someOtherClass.readCache(mockFile)
        self.assertEqual(result,"Returning mocked data")
        mockFile = MockFileManagerNotStored()
        someOtherClass = SomeOtherViewModel()
        result = someOtherClass.readCache(mockFile)
        self.assertEqual(result, None)

The full code snippet is here:

from unittest import mock, TestCase
from main import FileManager,SomeOtherViewModel


class MockFileManagerStored(FileManager):
    def isStored(self):
        return True

    def read(self):
        return "Returning mocked data"

class MockFileManagerNotStored(FileManager):
    def isStored(self):
        return False

    def read(self):
        return "Returning mocked data"

class FileTestCase(TestCase):

    def test_viewModel(self):
        mockFile = MockFileManagerStored()
        someOtherClass = SomeOtherViewModel()
        result = someOtherClass.readCache(mockFile)
        self.assertEqual(result,"Returning mocked data")
        mockFile = MockFileManagerNotStored()
        someOtherClass = SomeOtherViewModel()
        result = someOtherClass.readCache(mockFile)
        self.assertEqual(result, None)

Questions? Feel free to ask.