0

I am new to unit testing in Python. I am trying to unit test a method that does something like this:

MyClass:

    client : Client

    def __init__(client):
        self.client=client

    def method_to_test(self):
        images = self.client.get_stuff()
        image = images[0]
        result = []
        for region in image.regions:    
            result.append(region.region_id)
        return result

I have tried mocking and nesting of all the classes involved: Client, Image and ImageRegion. But at some point the mocking breaks down and my test fails. Is there a way to mock things so that one can do something like:

def test_method_to_test():
   result = MyClass(mock_client).method_to_test()
   assert dummy_region_id in result

... or perhaps better ways to test in such situations with nested custom objects?

My latest attempt looks like this:

with patch('package.module.ImageRegion') as MockRegion:
    region = MockRegion()
    region.region_id.return_value = dummy_region_id 
        with patch('package.module.Image') as MockImage:
            image = MockImage()
            image.regions.return_value = [region]
                with patch('package.module.Client') as MockClient:
                    mock_client = MockClient()
                    mock_client.get_regions.return_value = [image]
                    result = MyClass(mock_client).method_to_test()
                    assert dummy_region_id in result
Rafael Sanchez
  • 394
  • 7
  • 20

1 Answers1

0

You could create mocked object using unittest.mock and use Dependency Injection to pass the mocked object to MyClass.

E.g.

myclass.py:

class MyClass:

    def __init__(self, client):
        self.client = client

    def method_to_test(self):
        images = self.client.get_stuff()
        image = images[0]
        result = []
        for region in image.regions:
            result.append(region.region_id)
        return result

test_myclass.py:

import unittest
from unittest.mock import Mock
from collections import namedtuple
from myclass_63948475 import MyClass

Image = namedtuple('Image', ['regions'])
Region = namedtuple('Region', 'region_id')


class TestMyClass(unittest.TestCase):

    def test_method_to_test(self):

        regions = [Region('1'), Region('2'), Region('3')]
        images = [Image(regions=regions)]
        mock_client = Mock()
        mock_client.get_stuff.return_value = images
        myclass = MyClass(mock_client)
        actual = myclass.method_to_test()
        mock_client.get_stuff.assert_called_once()
        self.assertEqual(actual, ['1', '2', '3'])


if __name__ == '__main__':
    unittest.main()

unit test result with coverage report:

coverage run /Users/ldu020/workspace/github.com/mrdulin/python-codelab/src/stackoverflow/63948475/test_myclass_63948475.py && coverage report -m           
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
Name                                                  Stmts   Miss  Cover   Missing
-----------------------------------------------------------------------------------
src/stackoverflow/63948475/myclass_63948475.py           10      0   100%
src/stackoverflow/63948475/test_myclass_63948475.py      18      0   100%
-----------------------------------------------------------------------------------
TOTAL                                                    28      0   100%
Lin Du
  • 88,126
  • 95
  • 281
  • 483