Search code examples
pythonunit-testingmockingpython-unittestpython-mock

Mock out a local python variable


Let's say I have the following class:

class Person:
    def __init__(self, name):
        self.name = name

    def print_name(self):
        date = datetime.now().strftime('%Y-%m-%d')
        return 'Hello {}. Today is {}'.format(self.name, date)

I'm trying to mock out the date variable so I can write a successful test for the print_name method. I've been changing the function so date is returned from a function, which is easy to mock.

class Person:
    def __init__(self, name):
        self.name = name

    def print_name(self):
        date = self._get_date_now()
        return 'Hello {}. Today is {}'.format(self.name, date)

    @staticmethod
    def _get_date_now():
        return datetime.now().strftime('%Y-%m-%d')

And the unit tests would look like this:

from mock import patch

class MyTests(unittest.TestCase):

    @patch.object(Person, '_get_date_now')
    def test_hello(self, date_mock):
        date_mock.return_value = '2018-10-10'

        person = Person('Jim')
        result = person.print_name()
        self.assertEqual(result, 'Hello Jim. Today is 2018-10-10')

Is there anyway I can mock the date variable directly, or is this the best way to do it.

Note this is a simple example and creating the _get_date_now function is trivial. However, my use cases are much more complex and mocking out these local variables would be incredibly helpful.


Solution

  • use freezegun

    from freezegun import freeze_time
    
    @freeze_time("2018-10-10")
    def test_hello(self):
    
        person = Person('Jim')
        result = person.print_name()
        self.assertEqual(result, 'Hello Jim. Today is 2018-10-10')