Trying to mock datetime.date.today(), but not working Trying to mock datetime.date.today(), but not working python python

Trying to mock datetime.date.today(), but not working


Another option is to usehttps://github.com/spulec/freezegun/

Install it:

pip install freezegun

And use it:

from freezegun import freeze_time@freeze_time("2012-01-01")def test_something():    from datetime import datetime    print(datetime.now()) #  2012-01-01 00:00:00    from datetime import date    print(date.today()) #  2012-01-01

It also affects other datetime calls in method calls from other modules:

other_module.py:

from datetime import datetimedef other_method():    print(datetime.now())    

main.py:

from freezegun import freeze_time@freeze_time("2012-01-01")def test_something():    import other_module    other_module.other_method()

And finally:

$ python main.py# 2012-01-01


For what it's worth, the Mock docs talk about datetime.date.today specifically, and it's possible to do this without having to create a dummy class:

https://docs.python.org/3/library/unittest.mock-examples.html#partial-mocking

>>> from datetime import date>>> with patch('mymodule.date') as mock_date:...     mock_date.today.return_value = date(2010, 10, 8)...     mock_date.side_effect = lambda *args, **kw: date(*args, **kw)......     assert mymodule.date.today() == date(2010, 10, 8)...     assert mymodule.date(2009, 6, 8) == date(2009, 6, 8)...


There are a few problems.

First of all, the way you're using mock.patch isn't quite right. When used as a decorator, it replaces the given function/class (in this case, datetime.date.today) with a Mock object only within the decorated function. So, only within your today() will datetime.date.today be a different function, which doesn't appear to be what you want.

What you really want seems to be more like this:

@mock.patch('datetime.date.today')def test():    datetime.date.today.return_value = date(2010, 1, 1)    print datetime.date.today()

Unfortunately, this won't work:

>>> test()Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "build/bdist.macosx-10.6-universal/egg/mock.py", line 557, in patched  File "build/bdist.macosx-10.6-universal/egg/mock.py", line 620, in __enter__TypeError: can't set attributes of built-in/extension type 'datetime.date'

This fails because Python built-in types are immutable - see this answer for more details.

In this case, I would subclass datetime.date myself and create the right function:

import datetimeclass NewDate(datetime.date):    @classmethod    def today(cls):        return cls(2010, 1, 1)datetime.date = NewDate

And now you could do:

>>> datetime.date.today()NewDate(2010, 1, 1)