How to Mock the Current Date and Time in Python

The power of time in the palm of my hand!

If you’re testing Python code that relies on the current date or time, you will probably want to mock time to test different scenarios. For example, what happens when you run a certain piece of code on February 29? (A common source of bugs.)

Unfortunately such mocking is not so easy. If you try using unittest.mock to swap functions in the datetime module, you’ll hit a TypeError. For example this code:

import datetime as dt
from unittest import mock

with mock.patch.object(, "today",, 2, 29)):

…will blow up with this TypeError:

$ python
Traceback (most recent call last):
  File "/.../lib/python3.9/unittest/", line 1502, in __enter__
    setattr(, self.attribute, new_attr)
TypeError: can't set attributes of built-in/extension type ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/chainz/tmp/", line 4, in <module>
    with mock.patch.object(, 'today',, 2, 29)):
  File "/.../lib/python3.9/unittest/", line 1515, in __enter__
    if not self.__exit__(*sys.exc_info()):
  File "/.../lib/python3.9/unittest/", line 1521, in __exit__
    setattr(, self.attribute, self.temp_original)
TypeError: can't set attributes of built-in/extension type ''

This is because the datetime module is built in C, and unittest.mock only supports modifying pure Python objects and functions. So what are the alternatives?

One option is to refactor your code to use dependency injection, as suggested in this post by Haki Benita. But this can be time consuming, changes the layout of the code purely for the tests, and can be a large refactoring to impose on an otherwise working system.

Another option is to use a library that specifically mocks the date and time. These workaround the TypeError issue and also mock all of Python’s date and time functions at once to return consistent values.

One such library is time-machine, which I wrote earlier this year. With it, you can easily mock the current date and time by passing the point in time you’d like to end up at:

import datetime as dt

import time_machine

with, 2, 29)):

This works as expected:

$ python

time-machine was inspired by an earlier library called freezegun, with some improvements in terms of performance and consistency. For more information on the different ways you can use time-machine, see its README on PyPI.


Happy time travelling,


Improve your Django develompent experience with my new book.

Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts: