51

I'm trying to write tests for a class that has methods like:

import datetime
import pytz

class MyClass:
    def get_now(self, timezone):
        return datetime.datetime.now(timezone)

    def do_many_things(self, tz_string='Europe/London'):
        tz = pytz.timezone(tz_string)
        localtime_now = self.get_now(tz)
        ...
        return things

I want to test it, and to do so I need to make sure that the datetime.datetime.now() call returns something predictable.

I've been reading lots of examples of using Mock in tests, but haven't found anything quite like what I need, and I can't work out how to use it in my tests.

I separated the get_now() method out in case it's easier to mock that, instead of datetime.datetime.now(), but I'm still stumped. Any thoughts on how to write UnitTests for this using Mock? (This is all in Django, fwiw; I'm not sure if this makes a difference in this case.)

4
  • Just FYI, never use a pytz timezone in the datetime constructor. Use localize instead. Oct 25, 2012 at 18:05
  • Thanks Mark. So instead of datetime.datetime.now(timezone) I should do timezone.localize(datetime.datetime.now())? Any particular reason it's better? Oct 25, 2012 at 20:47
  • Sometimes assigning the timezone directly doesn't work properly. For one example see stackoverflow.com/questions/12808845/… Oct 25, 2012 at 21:20
  • Although, localize() is only used for naive datetimes. So if now() is 2012-10-26 15:00:00 (with no timezone) then it simply applies the specified timezone to it; it doesn't convert the time. As I want to get the actual time in a timezone, I think I need to do: datetime.datetime.now(pytz.utc).astimezone(timezone). Oct 26, 2012 at 10:43

8 Answers 8

45

You could use freezegun :

from freezegun import freeze_time

def test():
    assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
    with freeze_time("2012-01-14"):
        assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
    assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)

It basically mocks datetime module calls.

2
  • 1
    Thanks, It works! However the first assertion did not because the month token was not accepted in this format: 01 but datetime.datetime(2012, 1, 14) works.
    – Montaro
    Oct 16, 2016 at 14:36
  • 1
    freezegun is slow, especially if you test logic with multiple calls to datetime.now() Apr 29, 2019 at 23:30
34

You'd create a function that returns a specific datetime, localized to the timezone passed in:

import mock

def mocked_get_now(timezone):
    dt = datetime.datetime(2012, 1, 1, 10, 10, 10)
    return timezone.localize(dt)

@mock.patch('path.to.your.models.MyClass.get_now', side_effect=mocked_get_now)
def your_test(self, mock_obj):
    # Within this test, `MyClass.get_now()` is a mock that'll return a predictable
    # timezone-aware datetime object, set to 2012-01-01 10:10:10.

That way you can test if the resulting timezone-aware datetime is correctly being handled; results elsewhere should show the correct timezone but will have a predictable date and time.

You use the mocked_get_now function as a side-effect when mocking get_now; whenever code calls get_now the call is recorded by mock, and mocked_get_now is called, and it's return value used as the value returned to the caller of get_now.

2
  • @MartijnPieters can't able to import mock Dec 24, 2015 at 12:43
  • @AvinashRaj mock is an add-on package, install it with pip. With Python 3 it is included as unittest.mock.
    – Martijn Pieters
    Dec 24, 2015 at 13:37
5

I'm using date, but the same idea should work for datetime:

class SpoofDate(date):
    def __new__(cls, *args, **kwargs):
        return date.__new__(date, *args, **kwargs)

...

from mock import patch

@patch('some.module.date', SpoofDate)
def testSomething(self):
    SpoofDate.today = classmethod(lambda cls : date(2012, 9, 24))

Where some.module imports date. Patch is replacing the imported date with SpoofDate, which you can then redefine to do whatever you want.

3
  • @FearlessFuture did you just replace date with datetime? Can you write the implementation in datetime.
    – zakiakhmad
    Mar 11, 2015 at 14:00
  • @zakiakhmad, below is an example of what I did: class StubDate(datetime.datetime): pass @mock.patch("friend.datetime.datetime", StubDate) def test_generate_date(self): # Make datetime.datetime.now return a fixed value StubDate.now = classmethod(lambda cls: datetime.datetime(2015, 03, 11, 11, 01)) self.assertEqual( self.friend_obj.generate_date(input), datetime.datetime(2015, 03, 11, 11, 01)) > Blockquote Mar 11, 2015 at 15:06
  • @FearlessFuture thanks a lot! I wrote the gist here for a better readability. gist.github.com/za/2a217c47582737f88259
    – zakiakhmad
    Mar 12, 2015 at 0:40
5

Here's the most elegant way to do this IMO:

import datetime
from unittest import mock

test_now = datetime.datetime(1856, 7, 10)
with mock.patch('datetime.datetime', wraps=datetime.datetime) as dt:
    print(dt.now()) # calls the real thing
    dt.now.return_value = test_now
    print(dt.now()) # calls the mocked value

The advantages here is that you needn't patch the datetime module via the tested module's local attribute, it supports calling unmocked methods, and it doesn't require any external imports.

3

Having asked this question originally...

As @Jocelyn delalande suggested, I've been happily using freezegun for years now.

Another option is python-libfaketime, which can be much faster than freezegun, but doesn't work on Windows and sounds a bit fiddly.

A newer option is time-machine, introduced in this blog post that compares the three options.

2

I would use the helpers from the 'testfixtures' package to mock out the datetime class you're calling now() on:

http://packages.python.org/testfixtures/datetime.html#datetimes

That way, you can test all the cases you have, all the time.

1
  • 1
    Thank you, works much better and simpler than any other solutions Apr 29, 2019 at 23:30
1

Using patch of unittest.mock

from unittest.mock import patch

@patch('MyClass.datetime')
def test_foo(self, mock_datetime):
    mock_datetime.datetime.now.return_value = datetime.datetime(2019, 5, 7) #SOME_MOCKED_DATE

Note that we're overriding datetime module that is imported only in our class

The class for which we are writing the test:

import datetime

class MyClass:
    def foo():
       localtime_now = datetime.datetime.now(timezone)

We need not have to separate it as get_now() method just to make it easier to mock.

1

If you don't want to install anything this is the simplest way. Simply use, Mock class -

class NewDt(datetime.date):
    @classmethod
     def now(cls):
           return datetime.datetime.strptime('2020-07-10 05:20:20', '%Y-%m-%d %H:%M:%S')

And use this patch before mock function

 @mock.patch('module path', NewDt)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.