time-machine documentation¶
Travel through time in your tests.
A quick example:
import datetime as dt
from zoneinfo import ZoneInfo
import time_machine
hill_valley_tz = ZoneInfo("America/Los_Angeles")
@time_machine.travel(dt.datetime(1985, 10, 26, 1, 24, tzinfo=hill_valley_tz))
def test_delorean():
assert dt.date.today().isoformat() == "1985-10-26"
For a bit of background, see the introductory blog post and the benchmark blog post.
Testing a Django project? Check out my book Speed Up Your Django Tests which covers loads of ways to write faster, more accurate tests. I created time-machine whilst writing the book.
time-machine is a tool for mocking the time in tests.
To get started, see Installation, Usage, and pytest plugin. If you’re coming from freezegun or libfaketime, see Comparison and Migrating from freezegun or libfaketime.